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