Get all vms out of datastore

PowerShell script exchange

Get all vms out of datastore

Veeam Logoby dries.vergote » Thu Apr 06, 2017 7:54 am

Hi

How can I add vms to "Guest Processing -> Credentials... -> Guest OS Credentials".
We are user datastore in jobobject but for guest OS credentials I need to add vms to set credentials to it.

The part of setting the credentials I have already if I add the server through the GUI.

Thanks.
dries.vergote
Service Provider
 
Posts: 11
Liked: 1 time
Joined: Fri Apr 24, 2015 2:51 pm
Full Name: Dries Vergote

[MERGED] Get all vms out of datastore

Veeam Logoby dries.vergote » Thu Apr 06, 2017 12:24 pm

Hi
I would like to add all vm's in a datastore (added in job) to the guest OS credentials.
This is needed to specify the specific credentials of a vm. That part I already have with Set-VBRJobObjectVssOptions

Any idea how to accomplish this?
In the gui you can select the 'datastore and vms' and see only the vm's in that datastore.

thanks
dries.vergote
Service Provider
 
Posts: 11
Liked: 1 time
Joined: Fri Apr 24, 2015 2:51 pm
Full Name: Dries Vergote

Re: Get all vms out of datastore

Veeam Logoby tdewin » Thu Apr 06, 2017 1:49 pm

You can try this code
Code: Select all
$vmsandds = Find-VBRViEntity -DatastoresAndVMs
$ds = $vmsandds | ? { $_.name -eq "demo-fc" }
$vmsandds | ? { $_.Path.startswith($ds.path) -and $_.type -eq "Vm" }


(e.g "demo-fc" would be your datastore)
tdewin
Veeam Software
 
Posts: 1110
Liked: 391 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

Re: Get all vms out of datastore

Veeam Logoby tsightler » Thu Apr 06, 2017 2:12 pm

If the job has a container object you'll have to manually enumerate and add the VM objects to the job to set the passwords for each. I usually use something like:
Code: Select all
# Get VMs by datastore and vms view
$vmsbydatastorepath = Find-VBRViEntity -DatastoresAndVMs | ?{$_.Type -eq "Vm"}


Then you can find VMs that belong to a datastore with:
Code: Select all
($vmsbydatastorepath | ?{$_.Path -like "*<datastore_name>*"})


Admittedly, this code does assume you will have only one Datastore with than name across the entire environment, if you have multiple vCenters with the possibility of duplicate datastore names you'd want to make sure to use the full path, this was more as a conceptual example, and approaches it pretty much the same way Timothy's code does.

Of course, you then still need to add those objects to the job and switch the object type of vssChild and then set the credentials. I've used code something like this in the past:

Code: Select all
write-host "Setting VSS Credentials for" $vm.Name
Add-VBRViJobObject -Job $job -Entities $vm | Out-Null
$newoij = Get-VBRJobObject -Job $Job -Name $vm.Name
$newoij.Info.Type = [Veeam.Backup.Model.CDbObjectInJobInfo+EType]::VssChild
Set-VBRJobObjectVssOptions -Object $newoij -Credentials $cred | Out-Null
tsightler
Veeam Software
 
Posts: 4874
Liked: 1821 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: Get all vms out of datastore

Veeam Logoby dries.vergote » Thu Apr 06, 2017 2:33 pm

Thanks for the replies.
Indeed this works fine, I'll post update with full script when done.

For adding credentials per vm I got from Timotht to use another enum instead. Else you add it to the application only.

Code: Select all
[Veeam.Backup.Model.CDbObjectInJobInfo+EType]::GuestCredsChild
dries.vergote
Service Provider
 
Posts: 11
Liked: 1 time
Joined: Fri Apr 24, 2015 2:51 pm
Full Name: Dries Vergote


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 1 guest