You can use the Get-View cmdlet to update server-side objects.
Prerequisites
Verify that you are connected
to a
vCenter
Server
system.
Procedure
1 | Get the VM2 virtual machine by name. $vm2 = Get-View -ViewType VirtualMachine -Filter @{"Name" = "VM2"}
$vmhostView = Get-View -Id $vm2.Runtime.Host
|
2 | View the current power state. $vm2.Runtime.PowerState
|
3 | Power off the virtual machine. If ($vm2.Runtime.PowerState -ne “PoweredOn”) {
$vm.PowerOnVM($vm2.Runtime.Host)
} else {
$vm2.PowerOffVM()
}
|
4 | View the value of the $vm2 power state. $vm2.Runtime.PowerState
The power state is not updated yet because the virtual machine property values are not updated automatically.
|
5 | Update the view object. $vm2.UpdateViewData()
|
6 | Obtain the actual power state of the virtual machine. $vm2.Runtime.PowerState
|