VMware Cloud Community
DZ1
Hot Shot
Hot Shot

Setting an ESXi host to whatever state it was in before it was disconnected.



I'm writing a script to go through each ESXi host that is disconnected, and then the script will reconnect them.  I'm also logging the host name before and after the reconnection.  I've noticed that if the host was in maintenance mode while it's disconnected, the script will reconnect the host, but it will not be in maintenance mode any longer after connected.  If I right-clicked and choose to connect, the host would reconnect, and stay in maintenance mode.  Is there a way to dynamically find out what mode the host is in while it's technically disconnected?  So far it seems as if the Set-VMHost -state is an explicit setting, and going through the GUI and choosing "connect" on a disconnected host are two different things.  


$Disconnected = $null



$Disconnected = @()



$StillDisconnected = $null



$StillDisconnected = @()



$today = (Get-Date).ToString()



Get-VMHost | where { $_.connectionstate -eq "Disconnected"} | foreach {



$Disconnected += $_.name



"$today" + "`r`n" +  ($Disconnected | out-string) | Out-File c:\Output\Disconnected.txt -Append



Set-VMHost -VMHost $_ -State Connected



}



Get-VMHost | where { $_.connectionstate -eq "Disconnected"} | foreach {



$StillDisconnected += $_.name



"$today" + "`r`n" +  ($StillDisconnected | out-string) | Out-File c:\Output\StillDisconnected.txt -Append



}

0 Kudos
2 Replies
LucD
Leadership
Leadership

The 'Set-VMHost -State Connected' performs in fact the call to the ExitMaintenanceMode API as well.

If you want to connect the ESXi in the state it was before the disconnect, you can do

$esx = Get-VMHost -Name MyEsx

$esx.ExtensionData.ReconnectHost($null,$null)

Perhaps the PowerCLI Dev Team should consider adding a state, for example "AsIs", to the VMHostState enumeration.

That way all the options available in the vSphere client would also be available with the Set-VMHost cmdlet.

And yes, the maintenance status is kept on the ESXi servers as well. See KB9639912.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DZ1
Hot Shot
Hot Shot


No offense to anyone, but I should just title all my questions as "Hey LucD", you answer most of them before I finish typing.  Of course, thanks to everyone that replies as well, not just LucD.  Thanks for the info, and again, I wish I would have looked a little more.  I was looking at the $_.extensiondata and I missed that Method.  I really hope to ask less questions, or at least more difficult ones.  Thanks again for the help, and good luck on the new PowerCLI book.  

0 Kudos