View and PowerCLI – Part 2

Part 1

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
This entry was posted in Computing, PowerShell, Virtualization, VMware and tagged , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.