PowerCLI: Restart ESXi hosts in a cluster sequentially

below a script which allows you to restart the hosts in a vSphere cluster sequentially. It prompts you to select a cluster and it begins the reboot each host sequentially in a safe manner.

function RestartCluster ($cluster) {
    if ($cluster) {
        #select vmhosts from cluster
        $vmhosts = get-cluster $cluster | Get-VMHost 
        $vmhostcount = $vmhosts.Count
        if ($vmhostcount -ge 2) {
             $vmhostpercentage = 100/$vmhostcount   
             foreach ($vmhost in $vmhosts) {
                $percentagecomplete += $vmhostpercentage
                $vmhostobj = get-vmhost -Name $vmhost
                Write-Progress -Activity "restart cluster $($cluster.name)" -Status "rebooting vmhost $($vmhostobj.name)" -PercentComplete $percentagecomplete
            
                #enter maintenance mode/evacuate
                Set-VMhost $vmhostobj -State maintenance -Evacuate -VsanDataMigrationMode Full -Confirm:$false | Out-Null

                #wait for maintenance mode/evacuate
                do {
                    sleep 10
                    $ConnectionState = (get-vmhost $vmhostobj).ConnectionState
                }
                while ($ConnectionState -eq "Connected")

                #reboot vmhost
                Restart-VMHost -VMHost $vmhostobj -Confirm:$false |Out-Null

                #wait for not responding state
                do {
                    sleep 10
                    $ConnectionState = (get-vmhost $vmhostobj).ConnectionState
                }
                while ($ConnectionState -eq "Maintenance")

                #wait for maintenance mode state
                do {
                    sleep 10
                    $ConnectionState = (get-vmhost $vmhostobj).ConnectionState
                }
                while ($ConnectionState -eq "NotResponding")

                #exit maintenance mode
                Set-VMhost $vmhostobj -State Connected -Confirm:$false | Out-Null

                #wait for connected state
                do {
                    sleep 10
                    $ConnectionState = (get-vmhost $vmhostobj).ConnectionState
                }
                while ($ConnectionState -eq "Maintenance")
            }    
        }
        else {write-host "Cluster contain to less hosts for sequential reboots, use Restart-VMHost cmdlet instead"}
    }
}

$cluster = get-cluster | Out-GridView -Title "select cluster to reboot sequentially" -OutputMode Single

RestartCluster -cluster $cluster
Advertenties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google photo

Je reageert onder je Google account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s