VMware Cloud Community
mparent
Contributor
Contributor
Jump to solution

Clone vm's and wait-task

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?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

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.

View solution in original post

0 Kudos
2 Replies
admin
Immortal
Immortal
Jump to solution

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.

0 Kudos
mparent
Contributor
Contributor
Jump to solution

Ok, I can figure out a way of doing it. Probably just do a loop with a pause and check the powered state.

0 Kudos