Powershell to list Azure Server Backup Report from the Azure Recovery Service Vaults

Powershell to list Azure Server Backup Report from the Azure Recovery Service Vaults

I searched the internet for ideas and eventually wrote a PS script to retrieve the Azure Server backup details. I took the original script idea from the superuser site post https://superuser.com/questions/1716872/azure-server-backup-report and modified it to fit my needs, which worked well. The Script below is working fine and can be further modified to fit your needs.


<# 
    # Requires -Module Az
    # Requires PowerShell Version 7.3.1 or later
    
    # DESCRIPTION
        # This is regarding the getting the Azure Backup Report for the backup job status and publish it in csv and html formats.
    
    # INPUTS
        Mandatory
        TenantId - Azure AD TenantId which you want to connect. 
    
    # Example Usage
        Use PowerShell dot sourcing to run the script, e.g.

        Run this in PowerShell: ". 'C:\{ScriptLocation}\Get-AzVmBackupReport.ps1'"
        The dot notation will import the script into the current PowerShell window scope and make it's emebdeded functions available to be called

        Then call the function with the required parameters, e.g. Get-AzVmBackupReport -TenantId <azure ad tenantId>
#>
# Parameter binding
[CmdletBinding()]
param(
[String]$TenantId = ''
)

#! set the variables
$DateTime = Get-Date -Format ddMMyyyy-HHmm 
$Output_Path_CSV = "AzureBackupJobListReport" +"-" + "$DateTime" + ".csv"
$Output_Path_HTML = "AzureBackupJobListReport" +"-" + "$DateTime" + ".HTML"
$jobsAllArray = @()
$body = "<html>" `
      + "<body>" `
      + "<table>"

$emptyJob = ""
$i = 0

#! Connect to the Azure tenant and start the script execution. 

Try {
Write-Host "Connecting to Azure Cloud..."
Connect-AzAccount -TenantId $tenantId -ErrorAction Stop -WarningAction SilentlyContinue | Out-Null
}
Catch {
Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"
Break
}

#! get only the subscriptions which are within the connected tenant. 
$Subscription = Get-AzSubscription -TenantId $tenantId
foreach ( $Subscription in $Subscriptions ) {
    #! Set Azure Subscription Context
    Try {
    Write-Host "Setting Azure Context - Subscription Name: "($azSub.Name) "..."
    Set-AzContext -Subscription $azSub.Name 
    $SubscriptionId = $Subscription.SubscriptionId 
    }
    Catch {
    Write-Warning "Cannot set Azure context. Please check your Azure subscription name. Exiting!"
    Break
    }

    Try {
        Write-Host "Getting the existing Azure Recovery Services Vault..."
        
        $rcvaults= Get-AzRecoveryServicesvault
        $i++
        #¡ loop through each vault
        foreach ($rcvault in $rcvaults) {  
            #! fetch the recoery services vault context and obtain the job status 
            Get-AzRecoveryServicesvault -name $rcvault.Name | Set-AzRecoveryServicesVaultContext ;
            $JobStatus = Get-AzRecoveryServicesBackupJob -From (Get-Date).AddDays(-1).ToUniversalTime() | `
                        Select WorkloadName,Operation,Status,StartTime,EndTime;

            #! if the job status is empty or null, mark it and update the jobstatus array. 
            if ([string]::IsNullorEmpty($JobStatus)) {
                $emptyJob += $rcvault.Name + ", "
            }
            else {
                #! populate the html content and initialize it
                $body += "<tr>" `
                    + "<th colspan='5' style='background-color:SteelBlue; color: white;'>" `
                    + $rcvault.Name `
                    + "</th>" `
                    + "</tr>" `
                    + "<tr>" `
                    + "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Server Name</th>" `
                    + "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Operation</th>" `
                    + "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Status</th>" `
                    + "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>Start Time</th>" `
                    + "<th style='background-color:LightSteelBlue; color: white; padding:5px;'>End Time</th>" `
                    + "</tr>"
                $rowcount = 0
                #! loop through each backup job and fetch the status and add to the html content collection for final report.
                foreach($job in $JobStatus) {
                    if ($rowcount % 2 -eq 0 ) {
                        $body += "<tr>" `
                            + "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
                            + $Job.WorkloadName `
                            + "</td>" `
                            + "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
                            + $Job.Operation `
                            + "</td>"
                        if ($Job.Status -eq "Completed") {
                        $body += "<td style='background-color:Lime; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        elseif ($Job.Status -eq "InProgress") {
                            $body += "<td style='background-color:Moccasin; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        else {
                            $body += "<td style='background-color:Red; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        $body += "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
                            + $Job.StartTime `
                            + "</td>" `
                            + "<td style='background-color:PaleTurquoise; color: black; padding:5px; text-align: center;'>" `
                            + $Job.EndTime `
                            + "</td>" `
                            + "</tr>"
                        $rowcount++
                    }
                    else {
                        $body += "<tr>" `
                            + "<td style='background-color:LightCyan; color: black; padding:5px; text-align: center;'>" `
                            + $Job.WorkloadName `
                            + "</td>" `
                            + "<td style='background-color:LightCyan; color: black; padding:5px; text-align: center;'>" `
                            + $Job.Operation `
                            + "</td>"
                        if ($Job.Status -eq "Completed") {
                            $body += "<td style='background-color:Lime; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        elseif ($Job.Status -eq "InProgress") {
                            $body += "<td style='background-color:Moccasin; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        else {
                            $body += "<td style='background-color:Red; color: black; padding:5px; text-align: center;'>" `
                                + $Job.Status `
                                + "</td>"
                        }
                        $body += "<td style='background-color:LightCyan; color: black; padding:5px; text-align: center;'>" `
                            + $Job.StartTime `
                            + "</td>" `
                            + "<td style='background-color:LightCyan; color: black; padding:5px; text-align: center;'>" `
                            + $Job.EndTime `
                            + "</td>" `
                            + "</tr>"
                        $rowcount++
                    }
                    #¡ updating the array collection at end of each check. 
                    $jobsAllArray += New-Object PSObject -Property @{`
                        BackupVault=$rcvault.Name; `
                        ServerName=$Job.WorkloadName; `
                        Operation=$Job.Operation; `
                        Status=$Job.Status;`
                        StartTime=$Job.StartTime;`
                        SubscriptionName=$Subscription.Name;`
                        EndTime=$Job.EndTime;
                    }
                }
            }
            #¡ Get the count of each jobs based on their status. 
            $CompletedJobs= $jobsAllArray.Where({$_.Status -eq 'Completed'})
            $InprogressJobs= $jobsAllArray.Where({$_.Status -eq 'InProgress'})
            $FailedJobs= $jobsAllArray.Where({$_.Status -eq 'Failed'})
        }
    }
    Catch {
        Write-Warning "Cannot set Azure Recovery Services Vault Context. Please check your Azure Recovery Services Vault name. Exiting!"
        Continue
    }
} # loop end foreach ($azSub in $azSubs)

#¡ export the output of the details collected in csv and html format.
$jobsAllArray | Export-csv "$Output_Path_CSV" -Append -NoTypeInformation -Verbose
ConvertTo-HTML -Body $body | Out-File "$Output_Path_HTML"

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link