Hi
I would like to put an esxi host in maintenance mode, but also I would like to know when the ESXi is on maintenance mode, then reboot the esxi once in maintenance mode, and check when it comes back up to exit maintenance mode. Hope I make sense
connect-viserver
$esxihost="esxi"
get-vmhost -name $esxihost | set-Vmhost -state Maintenance
"I would like to check the status until esxi is on maintenance mode"
Restart-VMhost $esxihost
" check when comes up into maintenance mode"
get-vmhost-Name $esxihost | set-VMhost -state connected
How am I doing??
Thanks
No, a coded timeout.
Something like this for example
Connect-VIServer
$esxihost = "esxi"
$esx = Get-VMHost -Name $esxihost | Set-VMHost -State Maintenance
$start = Get-Date
$span = New-TimeSpan -Start $start -End (Get-Date)
while($esx.ConnectionState -ne 'Maintenance' -and $span.TotalSeconds -le $timeout){
sleep 5
$esx = Get-VMHost -Name $esx.Name
$span = New-TimeSpan -Start $start -End (Get-Date)
}
if($span.TotalSeconds -gt $timeout){
Write-Host "Host didn't go into maintenance in time"
return
}
Restart-VMHost $esxihost
$start = Get-Date
$span = New-TimeSpan -Start $start -End (Get-Date)
while($esx.ConnectionState -eq 'Maintenance' -and $span.TotalSeconds -le $timeout){
sleep 5
$esx = Get-VMHost -Name $esx.Name
$span = New-TimeSpan -Start $start -End (Get-Date)
}
if($span.TotalSeconds -gt $timeout){
Write-Host "Host didn't start in time"
return
}
}
Get-VMHostvmhost-Name $esxihost | Set-VMHost -State connected
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A While-loop would be one way to check for a state change.
Something like this for example.
One word of warning, if an ESXi has issues going into maintenance mode or coming out of maintenance mode, you could be stuck in an endless loop.
This could be avoided by implementing a timeout.
$esxihost = "esxi"
$esx = Get-VMHost -Name $esxihost | Set-VMHost -State Maintenance
while($esx.ConnectionState -ne 'Maintenance'){
sleep 5
$esx = Get-VMHost -Name $esx.Name
}
Restart-VMHost $esxihost
while($esx.ConnectionState -eq 'Maintenance'){
sleep 5
$esx = Get-VMHost -Name $esx.Name
}
Get-VMHostvmhost-Name $esxihost | Set-VMHost -State connected
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
L.
A timeout as start-sleep or wait-process?
Thanks
No, a coded timeout.
Something like this for example
Connect-VIServer
$esxihost = "esxi"
$esx = Get-VMHost -Name $esxihost | Set-VMHost -State Maintenance
$start = Get-Date
$span = New-TimeSpan -Start $start -End (Get-Date)
while($esx.ConnectionState -ne 'Maintenance' -and $span.TotalSeconds -le $timeout){
sleep 5
$esx = Get-VMHost -Name $esx.Name
$span = New-TimeSpan -Start $start -End (Get-Date)
}
if($span.TotalSeconds -gt $timeout){
Write-Host "Host didn't go into maintenance in time"
return
}
Restart-VMHost $esxihost
$start = Get-Date
$span = New-TimeSpan -Start $start -End (Get-Date)
while($esx.ConnectionState -eq 'Maintenance' -and $span.TotalSeconds -le $timeout){
sleep 5
$esx = Get-VMHost -Name $esx.Name
$span = New-TimeSpan -Start $start -End (Get-Date)
}
if($span.TotalSeconds -gt $timeout){
Write-Host "Host didn't start in time"
return
}
}
Get-VMHostvmhost-Name $esxihost | Set-VMHost -State connected
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
L.
as always thanksssssss!!!