1

I am looking for a way to check if a list of VMs has backup and what is the status. I managed to get the status of VM backups but if the VM was not found in the $tasks I am not getting error. I need to know if a VM is not present in the $tasks so that I know that no backup is configred for this VM. The script so far.

    Write-Host "Enter Backup Server" -ForegroundColor cyan
 

 
$h = read-host -Prompt 'Hostname'


Write-Host " "
write-host "Hostname--------Job-------------Status " -ForegroundColor Cyan
Foreach ($i in $Hostname) {
Invoke-Command -ComputerName $h -ScriptBlock {
Add-PSSnapin VeeamPSSnapin

 foreach($Job in (Get-VBRJob))
{
        $Session = $Job.FindLastSession()
        if(!$Session){continue;}
        $Tasks = $Session.GetTaskSessions()
        $Tasks | ?{$_.Name -eq $using:i} | %{write-host $_.Name ":",$_.JobName,"===>"$_.Status}
        
}}
}

Thanks in advance!

Valeri

Val
  • 73
  • 1
  • 1
  • 5

2 Answers2

0

You are nearly there, you just need to unify your exit clause so that whether a VM has backup or not, it outputs something similar. Then when you run against a list of VMs and one or two aren't backed up, they'll appear in the output in a way that makes sense.

Foreach ($i in $Hostname) {
    Invoke-Command -ComputerName $h -ScriptBlock {
        Add-PSSnapin VeeamPSSnapin

        foreach($Job in (Get-VBRJob))
        {
                $Session = $Job.FindLastSession()
                if(!$Session){
                    [pscustomObject]@{
                        computerName = $_.Name;
                        backupStatus = "Not backed up!";
                        jobs = $null;
                    }
                $Tasks = $Session.GetTaskSessions()
                $jobs = $Tasks | ?{  $_.Name -eq $using:i } | Select -ExpandProperty JobName
                
                [PSCustomObject]@{
                    computerName = $_.Name;
                    backupStatus = "Backed up";
                    jobs = ($jobs -join ",")
                }
        }
    }
}

With a few small tweaks, we now emit an object back if a machine doesn't have any results for $Session, but now also return a similar object for machines that do have backups enabled.

You will likely need to tweak the code to your desired results, as I don't have Veeam available I can't quite nail down what you'd like but this should get you going.

And as a perk getting PowrShell objects back is easier to work with. You can save them in a json file or csv, or run this automatically in the off hours and review the results later, all which are much easier than using Write-Host commands.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • Hello FoxDeploy! Thank you very much for the help. Unfortunately I am still little stuck. I cannot get the content of the pscustomeobject. I have Created my objects fro both PS custom objects but when I try to see what is in there I do not get anything. I use : $bkp=[PSCustomObject]@{ computerName = $_.Name; backupStatus = "Backed up"; jobs = ($jobs -join ",") } Write-Host ($bkp | ft | Out-String) also tried $bkp.computername but I do not get anything as well. – Val Apr 01 '21 at 09:55
  • Use `Write-Output` instead of `Write-Host`, as Write-Host strips formatting and should only be used when human eyes are viewing a terminal – FoxDeploy Apr 01 '21 at 13:26
  • Write-Output also do not return anything. alternativelyit will be enaught, if I can count the backed up VMs ($using:i) from my initial script. So I can comair the count of all VMs with the backed up and know if there is no backed up VMs. Unfortuanately I tried several things but with no success. Tried to increment variable $v++ can you give me some hint ? Many thanks in advance! – Val Apr 01 '21 at 19:40
0

I managed to acheave my goal by comairing all backed up VMs with the VMs from my list:

Write-host "Unprotected VMs (No backup)" -ForegroundColor RED
Write-host "---------------------------" -ForegroundColor RED
invoke-command -computername $h -ScriptBlock {
Add-PSSnapin VeeamPSSnapin
$backup=Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -or $_.JobType -eq "Replica" -and $_.EndTime}|foreach{$_.gettasksessions() | Where-Object {$_.Status -ne "Failed"}} |foreach{$_.Name} | Sort-Object | Get-Unique
$diff=Compare-Object $using:hostname $backup| ? { $_.SideIndicator -eq "<=" } | Select -ExpandProperty InputObject
$diff

As a result I am getting only the VMs which are missing from $backup and are present only in my list $hostnames .

Val
  • 73
  • 1
  • 1
  • 5