Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Created April 22, 2024 20:44
Show Gist options
  • Select an option

  • Save Rugby-Ball/a2f3451bfe1c530c08e3d3302c0e2c4a to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/a2f3451bfe1c530c08e3d3302c0e2c4a to your computer and use it in GitHub Desktop.
For Azure, get inventory of Recovery Point Backups showing Earliest and Latest and count of Recovery Points. #Utility #Public #Inventory #Audit #BackUp #Azure
# Azure-Recovery-Point-Inventory.ps1
<#
Description: For Azure, get inventory of Recovery Point Backups showing Earliest and Latest and count of Recovery Points.
Edited by: Ed Walsh
PowerShell.Core tested: Yes
MS-Graph: No
Version: 1.0.0
Create Date: 4/22/2024
Revised Date: 4/22/2024
#>
# You don't need the next line for every run of script, once a day should be enough.
# Connect-AzAccount
Clear-Host
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()
$stopwatch.Start()
Write-Host "Script started on $(Get-Date -f "MM-dd-yyyy hh:mm tt") This script can take sometime to run." -ForegroundColor Yellow -BackgroundColor Red
$timestamp = get-date -format yyyyMMddHHmmss
$subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else { "\Documents\" }
$mydocuments = $home + $subfolder
$fileName = "Azure-recovery-Point-Inventory-" + [string]$timestamp + ".csv"
$filePath = Join-Path $mydocuments $fileName
$out = @()
$rp = @()
$subscriptions = Get-AzSubscription
ForEach ($Subscription in $Subscriptions) {
Set-AzContext -SubscriptionId $Subscription.Id | Out-Null
$sub_id = $Subscription.id
$sub_name = $Subscription.Name
$RCvaults = get-azrecoveryservicesvault
ForEach ($RCvault in $RCvaults) {
Set-AzRecoveryServicesVaultContext -Vault $RCvault | Out-Null
$containers = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM"
foreach ($container in $containers) {
$backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType "AzureVM"
$recoveryPoints = Get-AzRecoveryServicesBackupRecoveryPoint -Item $backupItem
$RP += $recoveryPoints
$RP_count = ($recoveryPoints | Measure-Object).Count
$oldestRP = ($rP.RecoveryPointTime) | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum
$earliestRecoveryPoint = $recoveryPoints | Select-Object @{Name = "Subscription_ID"; e = { $sub_id } }, @{Name = "Subscription_Name"; e = { $sub_name } }, @{name = "Vault"; e = { $rcvault.name } }, @{n = "Container"; e = { $container.ResourceGroupName } }, @{n = "Computer"; e = { ($_.ItemName -replace '.*;') } }, @{n = "Earliest_RecoveryPoint"; e = { $oldestRP } }, @{n = "Latest_RecoveryPoint"; e = { $_.RecoveryPointTime } } , @{ n = "Backup_Count"; e = { $RP_count } }, * | Sort-Object RecoveryPointTime -Descending | Select-Object -First 1
$Out += $earliestRecoveryPoint
}
}
}
# $Out | Sort-Object Subscription_Name, vault | ForEach-Object { Write-Host "For Subscription" $_.Subscription_Name"in vault"$_.vault"container"$_.Container", the Earliest backup for"$_.computer"is"$_.Earliest_RecoveryPoint"and the current backup is"$_.RecoveryPointTime", and there are"$_.Backup_Count"backups." }
$out | Export-Csv -NoTypeInformation -Path $filepath
Write-Output "Exported to: $filePath"
Write-Output "Folder Path: $mydocuments"
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
$stopwatch.Stop()
$time = $stopwatch.Elapsed
Write-Host "Script finished on $(Get-Date -f "MM-dd-yyyy hh:mm tt"), Elapsed time to run script (HH:MM:SS.MS): $Time" -ForegroundColor Red -BackgroundColor Yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment