So I wanted to do some checks to a VM in my environment, but I realized there were a few issues – was the vm on? Was the guest responding? Could I make WMI calls?
Turned out RPC service was often hung, and the firewall is usually set to not respond to pings. However, the Remote Management service is enabled!
if (get-vm -Name "ThatVM" | where {$_.powerstate -eq "PoweredOn"}) {
#If on, make sure it's responding
$result = invoke-command -computername thatvm `
-scriptblock {get-service winrm} -EA SilentlyContinue
If ($result.status -ne "Running") {
#If not responding, restart. This assumes VM tools is running,
#next version will account for that also
Restart-VMGuest -VM "ThatVM" -confirm:$false | Out-Null
#wait
Start-Sleep -Second 5
#wait until the service is available
do{$result = invoke-command -computername thatvm `
-scriptblock {get-service winrm} -EA SilentlyContinue} until`
($result.status -eq "Running")
}
} else {
Start-VM -VM "ThatVM" -confirm:$false | Out-Null
#wait
Start-Sleep -Second 5
#wait until the service is available
do{$result = invoke-command -computername thatvm `
-scriptblock {get-service winrm} -EA SilentlyContinue} until`
($result.status -eq "Running")
}
#now I know the PC is up and remote management is available,
#assuming WinRM is set to to Auto