VMware Cloud Community
fborges555
Enthusiast
Enthusiast

ESXi Maintenance mode

HI

I would like to use a while loop to wait until the esxi is on maintenance mode to do something else

I have

get-vmhost -Name $esxiHost | set-vmhost -state "maintenance"

$result = (Get-VMHost -Name $esxiHost | select connectionstate)

while ($result.connectionstate -ne "Maintenance")

{

Start-Sleep -s 60

$result = (Get-VMHost -Name $esxiHost | select connectionstate)

}

is this right way to do this or is there another way, I am not sure if once the first statement is executed , the connection state changes to maintenance right away of if there is something in between until the esxi is fully in maintenance mode then that state changes.

Thanks

0 Kudos
3 Replies
LucD
Leadership
Leadership

Since PowerCLI .NET objects are not updated automatically, you will have to 'get' the object again.

And no, the ConnectionState does not change immediately after the Set-VMHost cmdlet, only when the ESXi node is actually in maintenance mode.

Your code is correct.

This would be my take on it.

$esx = Get-VMHost -Name $esxiHost | Set-VMHost -State "maintenance" -Confirm:$false

while ($esx.ConnectionState -ne "Maintenance")

{

    Start-Sleep -s 60

    $esx = Get-VMHost -Name $esx.Name

}


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

0 Kudos
fborges555
Enthusiast
Enthusiast

L.

so at this point been an object and all, I don't  have to pipe

$esx = Get-VMHost -Name $esx.Name | "select connectionstate "

correct???

0 Kudos
LucD
Leadership
Leadership

No, with Select-Object you get a different object.

You could do

$esxState = Get-VMHost -Name $esx.Name | select -ExpandProperty connectionstate

Then you have the state in the variable $esxState.

But I'm not sure why you would want to do that.


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

0 Kudos