I am working on a script to clone a vm a number of times. The way I want it to work is, I check if the source vm is on, if it is I want to shut it off, and have the script wait till it turns off. The problem I am having is with the Wait-task cmdlet. So something like this:
$vm = Get-VM $vmSrc
if ($vm.PowerState -eq "PoweredOn"){
Write-Host "$vmSrc is on, powering off before clone"
Shutdown-VmGuest -VM $vm | Wait-Task
Write-Host "Powered off $vmSrc"
}
However, Wait-task complains because I assume you can't pipe tasks to it?
I am working on a script to clone a vm a number of times. The way I want it to work is, I check if the source vm is on, if it is I want to shut it off, and have the script wait till it turns off. The problem I am having is with the Wait-task cmdlet. So something like this:
$vm = Get-VM $vmSrc
if ($vm.PowerState -eq "PoweredOn"){
Write-Host "$vmSrc is on, powering off before clone"
Shutdown-VmGuest -VM $vm | Wait-Task
Write-Host "Powered off $vmSrc"
}
However, Wait-task complains because I assume you can't pipe tasks to it?
Actually Shutdown-VMGuest doesn't return a task, but a VM guest. Thisis a limitation of the underlying API. Working around this problem may be possible, but it will be tricky as VMware tools stops running almost immediately after you call the API.
I am working on a script to clone a vm a number of times. The way I want it to work is, I check if the source vm is on, if it is I want to shut it off, and have the script wait till it turns off. The problem I am having is with the Wait-task cmdlet. So something like this:
$vm = Get-VM $vmSrc
if ($vm.PowerState -eq "PoweredOn"){
Write-Host "$vmSrc is on, powering off before clone"
Shutdown-VmGuest -VM $vm | Wait-Task
Write-Host "Powered off $vmSrc"
}
However, Wait-task complains because I assume you can't pipe tasks to it?
Actually Shutdown-VMGuest doesn't return a task, but a VM guest. Thisis a limitation of the underlying API. Working around this problem may be possible, but it will be tricky as VMware tools stops running almost immediately after you call the API.
Ok, I can figure out a way of doing it. Probably just do a loop with a pause and check the powered state.