Hi Friends,
I want to get snapshot of the vm then revert the vm with the snapshot.
But set-vm get erros in the script. How can do it?
$vm = Get-Folder -id Folder-group-v337 | Get-Vm -NoRecursion | where {($_ | Get-VMGuest).State -eq "NotRunning"}
get-snapshot -VM $vm -Name Baseline | set-vm -VM $vm
$vm | move-vm -Destination (Get-Folder -id Folder-group-v335)
Any suggestion is appreciate.
Since you didn't include the error message you're getting, I'll have to guess.
But it looks as if it could be related to arrays and the -Snapshot parameter on the Set-VM cmdlet.
The $vm variable can be an array containing multiple VirtualMachineImpl objects if the folder holds more than 1 guest where the VMware Tools are not running.
In this case the Get-Snapshot cmdlet can potentially return an array of SnapshotImpl objects.
But the Set-Vm cmdlet can only accept 1 SnapshotImpl object on the -Snapshot parameter (which in your script is passed via the pipeline).
To avoid this potential problem you could use a Foreach-Object cmdlet to loop through all the VirtualMachineImpl objects.
Get-Folder -id Folder-group-v337 | Get-Vm -NoRecursion | where {($_ | Get-VMGuest).State -eq "NotRunning"} | %{ get-snapshot -VM $_ -Name Baseline | set-vm -VM $_ $_ | move-vm -Destination (Get-Folder -id Folder-group-v335) }
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect. It can work.
Thanks for your great help.