pretty new to powershell as well as vmware and veeam backup. used this script to get backup information for our vms however, having difficulty exporting the results to .csv with following fields. vmname,backup_status,replication_status
any help/pointer is much appreciated.
Code: Select all
# Configuration
# vCenter server
$vcenter = "<vcenter_server>"
# To Exclude VMs from report add VM names to be excluded as follows
# $excludevms=@("vm1","vm2")
$excludevms=@()
# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null
# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
$excludedvms.Add($vm, "Excluded")
}
# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if (!$excludedvms.ContainsKey($vm.VMname)) {
$vms.Add($vm.VMname, "Unprotected")
}
}
# Find all backup job sessions that have ended in the last 24 hours
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-24)}
# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if($vms.ContainsKey($vm.VMname)) {
$vms[$vm.VMname]="Protected"
}
}
}
# Output VMs in color coded format based on status.
foreach ($vm in $vms.Keys)
{
if ($vms[$vm] -eq "Protected") {
write-host -foregroundcolor green "$vm is backed up"
} else {
write-host -foregroundcolor red "$vm is NOT backed up"
}
}