Sunday 24 July 2011

Fixing Disconnected VM's

When doing maintanance we have recently found that some VM's randomly dont reconnect to the network, and given the counts we have its a little hard to find them until the users complain that the servers are down, despite the fact they arent...

Anway I'm sure there is an underlying issue we need to fix but until them this is my solution, a powershell / powercli script.. it was tested with powercli 4.1.1-332441

# Ensure Networks are Connected on the currently powered On VM's
# DWallis 24/07/2011

# Adds the Powershell snapin's required if not already present.
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

# Ignore any SSL warnings
$WarningPreference = "SilentlyContinue"

#$viServerName = "viservernamehere"
connect-viserver $viServerName

# Connect to DataCenter

$ESXSRV = Get-DataCenter "DatacenterNameHere" | Get-VMHost

# Loop Through Each Powered On VM
Foreach ($VM in ($ESXSRV | Get-VM | where-object {$_.PowerState -eq "PoweredOn"}))
# Modify any un-connected VM's
$vm | Get-NetworkAdapter | Where { $_.connectionstate.connected -eq “$null” } | Set-NetworkAdapter -StartConnected:$true -Connected:$true -Confirm:$false

Disconnect-VIServer -Confirm:$False