PowerShell script exchange
chamdor
Influencer
Posts: 23
Liked: 1 time
Joined: Jun 15, 2009 7:00 pm
Contact:

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

Post by chamdor » Mar 16, 2010 3:57 am 1 person likes this post

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.

Gostev
Veeam Software
Posts: 22959
Liked: 2861 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

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

Post by Gostev » Mar 16, 2010 11:40 am 1 person likes this post

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 } }

chamdor
Influencer
Posts: 23
Liked: 1 time
Joined: Jun 15, 2009 7:00 pm
Contact:

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

Post by chamdor » Mar 16, 2010 1:38 pm

Awesome thanks. :-)

Andreas Neufert
Veeam Software
Posts: 2866
Liked: 499 times
Joined: May 04, 2011 8:36 am
Full Name: @AndyandtheVMs Veeam PM
Location: Germany
Contact:

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

Post by Andreas Neufert » 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}

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

v.Eremin
Veeam Software
Posts: 15139
Liked: 1140 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by v.Eremin » 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.

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

v.Eremin
Veeam Software
Posts: 15139
Liked: 1140 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by v.Eremin » 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.

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

v.Eremin
Veeam Software
Posts: 15139
Liked: 1140 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by v.Eremin » 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

 }
 

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

v.Eremin
Veeam Software
Posts: 15139
Liked: 1140 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

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

Post by v.Eremin » 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.

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

chris.childerhose
Veeam Vanguard
Posts: 87
Liked: 11 times
Joined: Aug 13, 2014 6:03 pm
Full Name: Chris Childerhose
Location: Toronto, ON
Contact:

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

Post by chris.childerhose » 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
Veeam Vanguard

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests