Powershell to get list of VM's in a Veeam backup job

PowerShell script exchange

Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chamdor » Tue Mar 16, 2010 3:57 am

Hi,

It doesn't appear that I can use Veeam's Powershell integration to get a list of VM's in a backup job. What I want to do ( in PowerShell ) is have a list of backup jobs, and the list of VM's in those jobs. Any tips on how to get the list of VM's in a job appreciated.
chamdor
Influencer
 
Posts: 23
Liked: never
Joined: Mon Jun 15, 2009 7:00 pm

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby Gostev » Tue Mar 16, 2010 11:40 am

Hello,

List of all backup jobs (that's an easy one) ;)
Code: Select all
Get-VBRJob

List of all VMs per job
Code: Select all
foreach($job in Get-VBRJob) { Write-Host "Job:", $job.Name; $job.GetObjectsInJob() | foreach { $_.Location } }
Gostev
Veeam Software
 
Posts: 21503
Liked: 2379 times
Joined: Sun Jan 01, 2006 1:01 am
Location: Baar, Switzerland

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chamdor » Tue Mar 16, 2010 1:38 pm

Awesome thanks. :-)
chamdor
Influencer
 
Posts: 23
Liked: never
Joined: Mon Jun 15, 2009 7:00 pm

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby Andreas Neufert » Fri Feb 27, 2015 2:54 pm

Just a v8 Update from a customer request...

List all VMs from a Job
Code: Select all
asnp VeeamPSSnapin
$JobnName = "YourJobName"
$JobObject = Get-VBRJob -name $JobnName
$Objects = $JobObject.GetObjectsInJob()
$Objects.name


List all Jobs
Code: Select all
asnp VeeamPSSnapin
$JobList = Get-VBRJob
$JobList.Name


List all VMs (unformated and undeduplicated) from all Jobs.
Code: Select all
asnp VeeamPSSnapin
$JobList = Get-VBRJob
foreach($Jobobject in $JobList)
{$Objects = $JobObject.GetObjectsInJob()
$Objects.name}
Andreas Neufert
Veeam Software
 
Posts: 2248
Liked: 374 times
Joined: Wed May 04, 2011 8:36 am
Location: Germany
Full Name: @AndyandtheVMs Veeam PM

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Mon Mar 02, 2015 4:47 pm

I am trying to use a script floating around that gives you job details but it does not have the Server names. How would I add this to the script?

Script -
Code: Select all
#--------------------------------------------------------------------
# Parameters
param (
   [Parameter(mandatory=$false)] [String]$path

)

#--------------------------------------------------------------------
# User Defined Variables

# Open csv file after creation
$autoLaunch = $false

#--------------------------------------------------------------------
# Static Variables

$scriptName = "VeeamJobDetail"
$scriptVer = "1.0"
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$starttime = Get-Date -uformat "%m-%d-%Y %I:%M:%S"
$allDetails = @()

#--------------------------------------------------------------------
# Load Snap-ins

# Add Veeam snap-in if required
If ((Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue) -eq $null) {add-pssnapin VeeamPSSnapin}

#--------------------------------------------------------------------
# Functions

#--------------------------------------------------------------------
# Main Procedures

Clear-Host
Write-Host "********************************************************************************"
Write-Host "$scriptName`tVer:$scriptVer`t`t`tStart Time:`t$starttime"
Write-Host "********************************************************************************`n"

# Get Backup Jobs
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

# Loop through each job adding details to array
foreach ($job in $jobs) {
   $jobOptions = New-Object PSObject
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value $job.isscheduleenabled
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Backup Mode" -value $job.backuptargetoptions.algorithm
   $repo = (Get-VBRBackupRepository | ?{$_.HostId -eq $job.TargetHostId -and $_.Path -eq $job.TargetDir}).name
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Repository" -value $repo
   $proxies = $null
   foreach ($prox in ($job | get-vbrjobproxy)) {
      $pName = $prox.Name
      $proxies = $proxies + $pName
   }
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Proxy" -value $proxies
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Auto Proxy" -Value $job.sourceproxyautodetect
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Next Run" -Value $job.scheduleoptions.nextrun
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Restore Points" -Value $job.backupstorageoptions.retaincycles
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Deduplication" -Value $job.backupstorageoptions.enablededuplication
   $comp = $job.backupstorageoptions.compressionlevel
   If ($comp -eq 0) {$comp = "None"}
   If ($comp -eq 4) {$comp = "Dedupe Friendly"}
   If ($comp -eq 5) {$comp = "Optimal"}
   If ($comp -eq 6) {$comp = "High"}
   If ($comp -eq 9) {$comp = "Extreme"}
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Compression" -Value $comp
   $opti = $job.backupstorageoptions.stgblocksize
   If ($opti -eq "KbBlockSize8192") {$opti = "Local Target(16TB+ Files)"}
   If ($opti -eq "KbBlockSize1024") {$opti = "Local Target"}
   If ($opti -eq "KbBlockSize512") {$opti = "LAN Target"}
   If ($opti -eq "KbBlockSize256") {$opti = "WAN Target"}
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Optimized" -Value $opti
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Integrity Checks" -Value $job.backupstorageoptions.enableintegritychecks
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Exclude Swap" -Value $job.visourceoptions.excludeswapfile
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Remove Deleted VMs" -Value $job.backupstorageoptions.enabledeletedvmdataretention
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Retain Deleted VMs" -Value $job.backupstorageoptions.retaindays
   $jobOptions | Add-Member -MemberType NoteProperty -Name "CBT Enabled" -Value $job.visourceoptions.usechangetracking
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Auto Enable CBT" -Value $job.visourceoptions.enablechangetracking
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Set VM Note" -Value $job.visourceoptions.setresultstovmnotes
   $jobOptions | Add-Member -MemberType NoteProperty -Name "VM Attribute Name" -Value $job.visourceoptions.vmattributename
   $jobOptions | Add-Member -MemberType NoteProperty -Name "VMTools Quiesce" -Value $job.visourceoptions.vmtoolsquiesce
   $jobOptions | Add-Member -MemberType NoteProperty -Name "VSS Enabled" -Value $job.vssoptions.enabled
   $igfs = $job.vssoptions.guestfsindexingtype
   If ($igfs -eq "None") {$igfs = "Disabled"}
   ElseIf ($igfs -eq "EveryFolders") {$igfs = "Enabled"}
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Index Guest FS" -Value $igfs
   $jobOptions | Add-Member -MemberType NoteProperty -Name "VSS Username" -Value $($job | get-vbrjobvssoptions).credentials.username
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Description" -Value $job.Description
   $allDetails += $jobOptions
}

#--------------------------------------------------------------------
# Outputs

# Display results summary
$allDetails | select Name, Enabled | Sort Name | ft -AutoSize

If (!$path -or !$path.EndsWith(".csv")) {
   Write-Host "`n`nUsing Default Path"
   $path = $scriptDir + "\" + $scriptName + "_" + (Get-Date -uformat %m-%d-%Y_%I-%M-%S) + ".csv"
   $path
} Else {
   Write-Host "`n`nUsing Supplied Path"
   $path
}

# Export results
$allDetails | Sort Name | Export-Csv $path -NoTypeInformation -Force

# Open csv
If ($autoLaunch) {
   Invoke-Item $path
}

$finishtime = Get-Date -uformat "%m-%d-%Y %I:%M:%S"
Write-Host "`n`n"
Write-Host "********************************************************************************"
Write-Host "$scriptName`t`t`t`tFinish Time:`t$finishtime"
Write-Host "********************************************************************************"

# Prompt to exit script - This leaves PS window open when run via right-click
Write-Host "`n`n"
Write-Host "Press any key to continue ..." -foregroundcolor Gray
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby v.Eremin » Mon Mar 02, 2015 5:04 pm

You need to place the following string inside the loop:

Code: Select all
$Objects = $Job.GetObjectsInJob()


And, then, assign $Objects as one of the properties of $JobOptions variable.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13543
Liked: 1002 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Mon Mar 02, 2015 5:13 pm

I put it in the loop and assigned it but getting unusual output in the CSV file. Is this right for adding the code to the loop?

Code: Select all
foreach ($job in $jobs) {
   $Objects = $Job.GetObjectsInJob()
   $jobOptions = New-Object PSObject
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -value $Objects


What this gives me for the Servers column -

Code: Select all
Veeam.Backup.Core.CObjectInJob[]
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby v.Eremin » Tue Mar 03, 2015 10:09 am

What happens if you try to assign only object names, not objects as a whole?

Code: Select all
foreach ($job in $jobs) {
$Objects = $Job.GetObjectsInJob() | select name
$jobOptions = New-Object PSObject
$jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
$jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -value $Objects


Thanks.
v.Eremin
Veeam Software
 
Posts: 13543
Liked: 1002 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Tue Mar 03, 2015 2:25 pm

When I do it this way I get mostly this in the column -

System.Object[]

I did get one to show this -

@{Name=SERVERA}

So a little closer but there is something missing. Any other thoughts?
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Tue Mar 03, 2015 2:38 pm

If I change this line I get one job showing the VM in the job but none of the others show which have more than 1 VM -

$jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -value $Objects

New Value

$jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -value $Objects.name

So not sure why it is not getting the jobs with multiple VMs to show.

UPDATE - So it appears jobs with multiple VMs this does not work on for some reason. Any job with 1 VM works perfect with this code update. Any thoughts?
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby v.Eremin » Tue Mar 03, 2015 2:55 pm

With the following code, even if there are multiple VMs chosen as a source for a backup job, all of them should be listed in the similar format:

Code: Select all
Name                                                                                                             Objects                                                                                                       
----                                                                                                             -------                                                                                                       
Backup Job 5                                                                                                     {HyperServ_replica, HyperServ_replica_temp}         


Code:

Code: Select all
foreach ($job in Get-VBRJob) {
   $jobOptions = New-Object PSObject
   
   $Objects = $Job | Get-VBRJobObject
   
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
   $jobOptions | Add-Member -MemberType NoteProperty -Name "Objects" -value $Objects.name

 }
 
v.Eremin
Veeam Software
 
Posts: 13543
Liked: 1002 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Tue Mar 03, 2015 3:12 pm

I tried changing the code and for some reason any job with multiple VMs it is not picking them up. No matter what I do.

I am running Powershell 2.0 - should it be updated to 3.0 for Veeam 8? Maybe there is something there but I cannot get the jobs with multiple VMs to spit them out in the CSV file. Grrrr :(
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby v.Eremin » Tue Mar 03, 2015 3:20 pm

Are those VMs selected individually or added through containers, such as Resource Pool, folder, hosts, etc.?

What happens if you query a job in question outside of this script?

Code: Select all
$Job = Get-VBRJob -name "Name of your backup job"
$Objects = $Job | Get-VBRJobObject
$Objects.name


Thanks.
v.Eremin
Veeam Software
 
Posts: 13543
Liked: 1002 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Tue Mar 03, 2015 3:37 pm

They are selected individually when added to the job from the VC. I use the CTRL key to select multiples when expanding my Cluster.

I will try the query outside the script and see.

UPDATE - How do I output that query to the screen? Sorry having brain fart this morning. This line seems to work just to query the jobs and displays the VMs in each job -

foreach($job in Get-VBRJob) { Write-Host "Job:", $job.Name; $job.GetObjectsInJob() | foreach { $_.Location } }
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Re: Powershell to get list of VM's in a Veeam backup job

Veeam Logoby chris.childerhose » Tue Mar 03, 2015 8:43 pm

I changed the line code for $Objects to this -

$Objects = $job | Get-VBRJobObject

This seems to work for adding the VMs to the column I need but still only pulls the single VM jobs and not jobs with more than 1 VM. Starting to pull my hair out now! :lol:
Chris Childerhose, VCAP-DCA, VMCE9, VCP, MCITP
chris.childerhose
Enthusiast
 
Posts: 72
Liked: 6 times
Joined: Wed Aug 13, 2014 6:03 pm
Location: Toronto, ON
Full Name: Chris Childerhose

Next

Return to PowerShell



Who is online

Users browsing this forum: No registered users and 18 guests