PowerShell script exchange
Post Reply
marc.charbonneau
Novice
Posts: 4
Liked: never
Joined: May 27, 2022 9:32 pm
Contact:

Start-VBRZip Backup - How to query job's list of VMs protected?

Post by marc.charbonneau »

I'm running the PS script at the bottom to protect some of our VMs with "Start-VBRZip".
Most of our VMs are protected with a standard job to be able to run incremental backups but I have a few VMs that only need infrequent protection using this script.
The VMs are assigned a VM tag in vSphere such as "VZIPTAGTEST1" and this script will identify those VMs and use "Start-VBRZip" to send them all to one VBK.
By sending these VMs to one VBK, they get the benefit of dedupication, unlike the way this script was originally written, which had each VM going to its own VBK.

The contents of this variable:
$ZIPSession
If I write it to screen with:
Write-Host $ZIPSession
Is:
Veeam.Backup.Core.CBackupSession

My question:
Does anyone know how to get the list of VMs protected from this job?
I figure that there's a way to get the list of VM from "Veeam.Backup.Core.CBackupSession" somehow but not finding any way.

If I run the following, none of the Properties returned have any values showing the VMs protected.
If it's available, it'll need to be queried another way.
$ZIPSession | Get-Member -MemberType Property | ForEach-Object {$propertyName = $_.Name
$propertyValue = $ZIPSession.$propertyName
Write-Host "$propertyName $propertyValue"
}

Thanks for any help.

------------------------------------------------------------------------------------------------------------------
--------------------PS Script to backup VMs with specific tag using "Start-VBRZip"--------------------
# Connect to vCenter
Get-VICredentialStoreItem -User "vsphere.local\minimum-rights-user"
Connect-VIServer vcenter.acme.com
$VMnames=Get-VM -Tag VZIPTAGTEST1 | Select-Object -ExpandProperty Name
$HostName = "vcenter.acme.com"
$CompressionLevel = "9"
$EnableQuiescence = $False
$EnableEncryption = $False
#$EncryptionKey = ""
$Retention = "In1Month"
# Enable notification (Optional)
$EnableNotification = $True
# Email SMTP server
$SMTPServer = "10.10.10.10"
# Email FROM
$EmailFrom = "VeeamZIP-Script@acme.com"
# Email TO
$EmailTo = "it@acme.com"
# Email subject
$EmailSubject = "VeeamZIP Job Report"
# Email formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"


$Server = Get-VBRServer -name $HostName
$MesssagyBody = @()
$VeeamVMs = Find-VBRViEntity -Name $VMNames -Server $Server
If ($EnableEncryption)
{
$EncryptionKey = Add-VBREncryptionKey -Password (cat $EncryptionKey | ConvertTo-SecureString)
$ZIPSession = Start-VBRZip -Entity $VeeamVMs -BackupRepository “ZIP-REPO1” -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention -EncryptionKey $EncryptionKey
}

Else
{
$ZIPSession = Start-VBRZip -Entity $VeeamVMs -BackupRepository “ZIP-REPO1” -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention
}

If ($EnableNotification)
{
$TaskSessions = $ZIPSession.GetTaskSessions().logger.getlog().updatedrecords
$FailedSessions = $TaskSessions | where {$_.status -eq "EWarning" -or $_.Status -eq "EFailed"}

if ($FailedSessions -ne $Null)
{
$MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e=
{$_.EndTime}},Result,@{n="Details";e={$FailedSessions.Title}})
}

Else
{
$MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e {$_.EndTime}},Result,@{n="Details";e={($TaskSessions | sort creationtime -Descending | select -first 1).Title}})
}
}

If ($EnableNotification)
{
$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $True
$message.Body = $MesssagyBody | ConvertTo-Html -head $style | Out-String
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP.Send($Message)
}

Disconnect-VIServer -Confirm:$false
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Start-VBRZip Backup - How to query job's list of VMs protected?

Post by david.domask »

Hi @marc.charbonneau,

Try just printing $ZipSession instead of Write-Host. Write-Host does some funky stuff and it's printing the object Type in your code. The result of the Start-VBRZip command is the CBackupSession object; you can pass this to Get-VBRTaskSession and see what is queued there. (ignore the failures, I just cancelled the job for speed purposes)

PS C:\Users\Administrator> $ZipSession

Job Name State Start Time End Time Result
-------- ----- ---------- -------- ------
Backup_2023-06-30... Stopped 6/30/2023 12:00:18 PM 6/30/2023 12:04:12 PM Failed


PS C:\Users\Administrator> Get-VBRTaskSession -Session $ZipSession

Name Status Operation
---- ------ ---------
ddom-tinyvm_replica Failed
ddom-tinyvm Failed
ddom-tinyvm_replica Failed
DDom-TinyVM_replica Failed
David Domask | Product Management: Principal Analyst
marc.charbonneau
Novice
Posts: 4
Liked: never
Joined: May 27, 2022 9:32 pm
Contact:

Re: Start-VBRZip Backup - How to query job's list of VMs protected?

Post by marc.charbonneau »

Hi @david.domask,
Thanks for the tip!
This worked well to have the e-mail notice now include status info on each VM that was protected.
Cheers,
Marc

------------------------------------------------------------------------------------------------------------------
--------------------PS Script to backup VMs with specific tag using "Start-VBRZip"--------------------
# Connect to vCenter
Get-VICredentialStoreItem -User "vsphere.local\minimum-rights-user"
Connect-VIServer vcenter.acme.com
$VMnames=Get-VM -Tag VZIPTAGTEST1 | Select-Object -ExpandProperty Name
$HostName = "vcenter.acme.com"
$CompressionLevel = "9"
$EnableQuiescence = $False
$EnableEncryption = $False
#$EncryptionKey = ""
$Retention = "In1Month"
# Enable notification (Optional)
$EnableNotification = $True
# Email SMTP server
$SMTPServer = "10.10.10.10"
# Email FROM
$EmailFrom = "VeeamZIP-Script@acme.com"
# Email TO
$EmailTo = "it@acme.com"
# Email subject
$EmailSubject = "VeeamZIP Job Report"
# Email formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"


$Server = Get-VBRServer -name $HostName
$MesssagyBody = @()
$VeeamVMs = Find-VBRViEntity -Name $VMNames -Server $Server
If ($EnableEncryption)
{
$EncryptionKey = Add-VBREncryptionKey -Password (cat $EncryptionKey | ConvertTo-SecureString)
$ZIPSession = Start-VBRZip -Entity $VeeamVMs -BackupRepository “ZIP-REPO1” -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention -EncryptionKey $EncryptionKey
}

Else
{
$ZIPSession = Start-VBRZip -Entity $VeeamVMs -BackupRepository “ZIP-REPO1” -Compression $CompressionLevel -DisableQuiesce:(!$EnableQuiescence) -AutoDelete $Retention
}


#########################################################
### New Changes to include each VM's Job Info in Email Notice ###
#########################################################
$JobSessions = Get-VBRTaskSession -Session $ZIPSession
$JobInfo = foreach ($jobSession in $JobSessions) {
$jobName = $jobSession.Name
$jobStatus = $jobSession.Status

$ZIPSession | Select-Object @{
n = "Name"
e = { ($_.name).Substring(0, $_.name.LastIndexOf("(")) }
},
@{
n = "Start Time"
e = { $_.CreationTime }
},
@{
n = "End Time"
e = { $_.EndTime }
},
@{
n = "Result"
e = { $_.Result }
},
@{
n = "Details"
e = { ($TaskSessions | Sort-Object CreationTime -Descending | Select-Object -First 1).Title }
},
@{
n = "Job Name"
e = { $jobName }
},
@{
n = "Job Status"
e = { $jobStatus }
}
}




If ($EnableNotification)
{
$TaskSessions = $ZIPSession.GetTaskSessions().logger.getlog().updatedrecords
$FailedSessions = $TaskSessions | where {$_.status -eq "EWarning" -or $_.Status -eq "EFailed"}

if ($FailedSessions -ne $Null)
{
$MesssagyBody = $MesssagyBody + ($ZIPSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e=
{$_.EndTime}},Result,@{n="Details";e={$FailedSessions.Title}})
}

Else
{
### Updated to improve e-mail notice info ###
$MesssagyBody = $MesssagyBody + $JobInfo
}
}

If ($EnableNotification)
{
$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $True
$message.Body = $MesssagyBody | ConvertTo-Html -head $style | Out-String
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP.Send($Message)
}

Disconnect-VIServer -Confirm:$false
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Start-VBRZip Backup - How to query job's list of VMs protected?

Post by david.domask »

Hi @marc.charbonneau,

Great! Glad it worked out, and thank you very much for sharing your script with the community! It's always appreciated to see more scripts to help everyone out :)
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests