PowerCLI

 View Only
  • 1.  CLI to power down VMs and place host in Maintenance Mode.

    Posted Apr 08, 2024 05:31 PM

    All: I'm looking for some assistance with our system, it's a DoD system and so the configuration was decided by others and cannot be modified. It may not be ideal, but it's what I have to work with.

    I have a vSphere 7.0U3 system with 2 hosts, they are each in their own cluster so that means that they have VCLS VMs running on them. We use a script to configure and patch them. The script creates a directory on the datastore, copies the patch VIBs, shuts down all vms, places the host in maintenance mode, installs the patch, then reboots the host. The hosts do not share any storage, DRS and HA are not enabled. vCenter is running on host 1 and is powered on when host 2 is being updated.

    The issue I'm having is that the PowerCLI command to place it in maint mode fails because VMs are powered on. It turns out that it is the VCLS VM. I run the command to power off all VM's before entering MM, but the VCLS VM powers back almost immediately and the Enter MM command fails.

    How can I get the VMs powered off and the host in Maintenance Mode?

    Here is the code I'm using:

    #Shutdown all VM's on the Host gracefully and if that fails perform a force shutdown

    Write-Host "Powering off the VMs on $ESXiHost gracefully." -ForegroundColor Yellow

    Get-VMHost $ESXiHost | Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'} | Shutdown-VMGuest -Confirm:$false -ErrorAction SilentlyContinue

    $continue = $false

    while($continue -eq $false){

    $VMCount = Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"}

    if ($VMCount.count -eq 0){

    $continue = $true

    }

    else{

    Write-Host "Waiting for the VMs to Power Off" -ForegroundColor Yellow

    Try{

    Get-VMHost $ESXiHost | Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'} | Shutdown-VMGuest -Confirm:$false -ErrorAction SilentlyContinue

    }

    catch {

    Get-VMHost $ESXiHost | Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'} | Shutdown-VMGuest -Confirm:$false -ErrorAction SilentlyContinue

    If($error.count -gt 0){

    Get-VMHost $ESXiHost | Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'} | Stop-VM -Confirm:$false -RunAsync -Kill

    }

    }

    }

    }

    #Place ESXi Host into Maintenance Mode

    try{

    Write-Host "Placing ESXi Host into Maintenance Mode." -ForegroundColor Yellow

    $Null = Set-VMHost -VMHost $ESXiHost -State Maintenance

    Write-Host "Host Entering Maintenance Mode. - Complete" -ForegroundColor Green

    } catch {

    $_.Exception

    Exit-Script

    }



  • 2.  RE: CLI to power down VMs and place host in Maintenance Mode.

    Posted Apr 08, 2024 06:16 PM


  • 3.  RE: CLI to power down VMs and place host in Maintenance Mode.

    Posted Apr 17, 2024 02:33 PM

    Thanks, I will try this.