PowerShell script exchange
Post Reply
PaulHill
Novice
Posts: 4
Liked: never
Joined: Aug 08, 2014 1:43 pm
Full Name: Paul Hill
Contact:

List VMs in a backup job when using folders not VMs

Post by PaulHill »

Case # 00614407

Hi,

I want to produce a report of VMs that are in backup jobs but I have specified a folder name not the individual VMs.

This means that I need to find the link between the objects in Veeam and the same object in VMWare

I can get a list of objects in each job using

get-VBRJob | where-object {$_.IsBackup} |get-vbrjobobject

But some of these are folders not VMs.

How do I identify which are folders and then list the VMs in them.

From the above I get an ID like "dedd496d-2666-49f8-9012-421d0b38d6bf" but

get-vm servername

gives me something like "VirtualMachine-vm-531"


Thanks
Paul
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by veremin »

A job object has a parameter called IsFolder, using which you can understand whether an object is a folder or not. For folder source objects you should utilize PowerCLI. Get a folder object (VeeamPSSnapin), connect to a given host/vCenter (PowerCLI), and check there what VMs are located within said folder(PowerCLI). Thanks.
PaulHill
Novice
Posts: 4
Liked: never
Joined: Aug 08, 2014 1:43 pm
Full Name: Paul Hill
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by PaulHill »

Hi,

Thanks for the update but IsFolder is always false.

Any other suggestions?

Thanks
Paul
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by veremin »

You can also try info.isfolder and see whether it makes any difference. Or try to play with location parameter in order to understand whether an object is folder or not. The last measure might be just collecting names of objects included in jobs and comparing these names with vSphere objects (PowerCLI). Such an approach should provide a definitive answer for the given question (is folder?).

Thanks.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by tsightler »

Below is some code I use to parse out VMs in a folder. The code as written assumes that the job only has a single folder, but it could be easily modified to support multiple folders in a job. Basically it uses Find-VBRViEntity to grab a list of all VMs and their paths into an array, it then looks at the job, grabs the folder object, creates the path, and uses that as a filter to select on the VMs that are in that folder.

Code: Select all

asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

$jobname = "<Job_Name>"

# Get entire vCenter VM hierarchy using VM and Template view for object [ath
$vmsandtemplates = Find-VBRViEntity -VMsAndTemplates
$vmfoldertree = $vmsandtemplates |? {$_.Type -eq "Vm"}
$vmfolders = $vmsandtemplates |? {$_.Type -eq "Folder"}

# Get Backup Job
$job = Get-VBRJob -Name $jobname | Sort -Property Name

write-Host $job.Name
# Get all included objects in job (assumes single folders)
$jobobjs = $job.GetObjectsInJob() | ?{$_.Type -eq "Include"}
# Get path for folder object
$jobobjid = $jobobjs.GetObject().Info.HostId.ToString() + "_" + $jobobjs.GetObject().Info.ObjectId
$jobobjpath = ($vmfolders | ?{$_.Id -eq "$jobobjid"}).Path
write-host $jobobjpath
# Get subset of VMs that are in the folder
$vmsinfolder= $vmfoldertree |?{$_.Path -like "$jobobjpath*"} | Sort -Property Name

ForEach ($vm in $vmsinfolder) {
    write-host "   " $vm.Name
}
I'm sure it's not exactly what you need, but hopefully it my be useful as one approach to accomplish this. I decided to use Find-VBRViEntity because that queries the VMs in the folder from vCenter, so the code works even if the job has never been run (so you can't query VMs in backups), or if VMs have been added or removed from the folder since the last run.

One thing the code doesn't do is take into account any excludes, but yet again this would be pretty easy to figure out. It would really be nice if there was a method within the job object to return the current list of VM objects based on the objects and excludes in the job, but for now I think you're stuck parsing this manually.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by tsightler »

Oh, and to answer you original question, you can easily tell the type of objects in job objects by querying the ViType parameter of the object so, for example, of $jobobj contains a Veeam job object you can get the type with:

Code: Select all

$jobobj.Object.ViType
If it's a VM the ViType is "Vm" if its a folder the ViType is "Folder". The Object property also includes values such as the objects MoRef which can be used to uniquely find the object in vCenter (the same as I'm doing in the above code).
PaulHill
Novice
Posts: 4
Liked: never
Joined: Aug 08, 2014 1:43 pm
Full Name: Paul Hill
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by PaulHill »

Hi,

Thanks for the suggestions and the code. I've made a few changes to cater for multiple folders and it's working fine.

Couldn't find VIType but could get $obj.Object.Type to tell me it's a Directory or VM.

Seems to be a convoluted way to get the information but it's working in our setup.

Not sure what would happen if we had folders within folders if that's possible. I might give that a try when I get the chance.

Thanks again
Paul
veremin
Product Manager
Posts: 20413
Liked: 2301 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by veremin »

PaulHill wrote:Couldn't find VIType but could get $obj.Object.Type to tell me it's a Directory or VM.
Sometimes parameters might be missing due to the absence of the latest Windows Manager Framework. (most commonly, on the default version of powershell on Windows 2008R2). Thus, it's always recommended to install, at least, Windows Framework 3.0.

Thanks.
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by tsightler »

The code above should work fine for VMs within nested folders as it considers any VM underneath that folder hierarchy as "included" in the folder even if the VM is in a subfolder. This is the same way the job would behave so I considered this when creating the job. It's certainly possible to make this more complete and become a full object parsing routine (i.e. get the list of job objects and turn it into a list of VMs, including excludes, etc), but the code would get far more complex since it would need to deal with all container style objects (resource pools, datastores, etc.) and I try to code to the simplest method that will get the job done.
PaulHill
Novice
Posts: 4
Liked: never
Joined: Aug 08, 2014 1:43 pm
Full Name: Paul Hill
Contact:

Re: List VMs in a backup job when using folders not VMs

Post by PaulHill »

Hi,

Thanks my reports working fine now.

I haven't consider datastores etc because we don't work that way, so it's fairly simple.

I'm with you, keep it simple.

Best Regards
Paul
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 18 guests