VMware Cloud Community
wanax1
Contributor
Contributor

get-vmguest then move-vm

Hi Friends,

I want to get the funcation, if the state of the guest is notrunning, then move the vm.

Get-Folder | Get-Vm | Get-Vmguest | where-object { $_.State -eq "NotRunning" } | move-vm.

but move-vm is failed. How can I do it?

Regards,

Gary

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

There are 2 points you have to take into account:

1) The Move-Vm cmdlet requires a VirtualMachineImpl object for the -Vm parameter.

This parameter can be passed via the pipeline.

But the Get-VmGuest cmdlet places a VMGuestImpl object in the pipeline.

2) The Move-Vm cmdlet requires a -Destination parameter

So this should do what I suspect you wanted to do

Get-VM  | where {$_.PowerState -eq "PoweredOff"} | Move-VM -Destination (Get-VMHost -Name <ESX-hostname>)

Or is there a specific reason why you want to test the state of the guest OS instead of the powerstate of the guest ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
wanax1
Contributor
Contributor

Thanks for your reply.

But the state of guest os doesn't eq the powerstate of the vm.

For example, if the guest os is standby state then the get-vmguest gets the state "NotRunning", but the same time the get-vm gets the powerstate "PoweredOn".

I want to move vm when the guest os is standby state.

Reply
0 Kudos
LucD
Leadership
Leadership

No problem, that can be done as well.

Get-VM | where {($_ | Get-VMGuest).State -eq "NotRunning"} | Move-VM -Destination (Get-VMHost -Name <ESX-hostname>)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
wanax1
Contributor
Contributor

Great, it can work.

Thanks.

Reply
0 Kudos