VMware Cloud Community
tdubb123
Expert
Expert

why is this not working

connect-viserver vc -credential (import-clixml "c:\scripts\adm.cred") -force

$DC = read-host "Enter the Datacenter to patch"

$vmhost = get-vmhost -Location (Get-Datacenter $dc)

$VMs = get-datacenter $dc | get-vm | ?{$_.powerstate -eq "Poweredon"}

$baseline = get-baseline -server vc -Name *Critical*

 

foreach ($vm in $VMs) {

 

  Shutdown-VMGuest -VM $vm -Confirm:$false

 

$counter = 0

 

   while($vm.PowerState -eq 'PoweredOn'){

 

if ($counter -gt 180) {

 

$vm = Get-VM -Name $vm.Name

 

stop-vm -vm $vm -confirm:$false

 

} else { 

 

sleep 5

 

   $vm = Get-VM -Name $vm.Name

 

$counter+=5

 

}

 

   }

 

   }

 

 

 

set-vmhost -VMHost $vmhost -State Maintenance -Confirm:$false

 

$baseline | Attach-Baseline -entity $vmhost -Confirm:$false

 

Scan-Inventory -Entity $vmhost

 

#$baseline | Remediate-Inventory -Entity $vmhost -Confirm:$false -ErrorAction SilentlyContinue

 

#get-vmhost -name $vmhost | set-vmhost -state Connected -Confirm:$false

 

#$vm | start-vm -confirm:$false

 

$UpdateTask = Update-Entity -Baseline $baseline -Entity $vmhost -RunAsync -Confirm:$false -ErrorAction Stop

while ($UpdateTask.percentcomplete -ne 100) {

 

    Write-Progress -Activity "Patching $vmhost" -PercentComplete $updatetask.PercentComplete -Status "$($UpdateTask.PercentComplete) %"

 

    Start-Sleep 5

 

    $Updatetask = Get-Task -Id $Updatetask.id

    Write-Output $updatetask

}

get-vmhost -name $vmhost | set-vmhost -state Connected -Confirm:$false

$vms | start-vm -Confirm:$false

 

 

----------

 

All the VMs are shutdown a but the script keeps showing this

 

tdubb123_0-1627227729993.png

 

0 Kudos
6 Replies
tdubb123
Expert
Expert

it it not putting the host in maintenance mode even after vms are all shutdown. stuck in the while loop or stop-vm

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Can you try with this snippet?

Get-Datacenter $dc | Get-VM | Where { $_.powerstate -eq "Poweredon" } |
ForEach-Object -Process {
    $vm = $_
    $counter = 0
    Shutdown-VMGuest -VM $_ -Confirm:$false
    while ($vm.PowerState -eq 'PoweredOn' -and $counter -le 180){
        sleep 5
        $vm = Get-VM -Name $vm.Name
        $counter += 5
    }
    if($counter -gt 180 -and $vm.PowerState -ne 'PoweredOff'){
        Stop-VM -VM $vm -Confirm:$false
    }
}


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

0 Kudos
tdubb123
Expert
Expert

thanks Luc. Yes this works. I wonder why my script did not get out of while loop. Also Is it possible to shutdown all vms async?

0 Kudos
LucD
Leadership
Leadership

I suspect that was because you were changing the foreach variable ($vm) inside the loop.

Afaik, the Shutdown-VMGuest (alias for Stop-VMGuest) doesn't allow a RunAsync.
But the cmdlet returns immediately after sending the shutdown request to the Guest OS.
You could send it to all VM, then in a loop, and for a certain time, wait till all VMs are powered down.
If the timeout expires, you could run the Stop-VM for all remaining VMs.

Something like this for example

Get-Datacenter $dc | Get-VM | Where { $_.powerstate -eq "Poweredon" } |
Shutdown-VMGuest -Confirm:$false

$counter = 0
while((Get-Datacenter $dc | Get-VM | Where { $_.powerstate -eq "Poweredon" }).Count -ne 0 -and $counter -le 180){
    sleep 5
    $counter += 5
}

Get-Datacenter $dc | Get-VM | Where { $_.powerstate -eq "Poweredon" } |
Stop-VM -Confirm:$false


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

0 Kudos
tdubb123
Expert
Expert

Hi Luc

 

I am trying to hosts on a cluster and here are the requirements

 

1. set the host in maintenance mode

2. make sure all vms are evacuated

3. Then manually power off one appliance vm which is pinned to the host

4. when that vm is shutdown, check if host in maintenance mode

5. then patch the host with lifecycle manager

6. wait for host to come back online and set it to exist maintenance mode

7. manually power on thast appliance vm

8. proceed with next host

 

Here is my script

 

import-module Vmware.VimAutomation.Core
import-module Vmware.Vumautomation


$vc = read-host "Enter the vcenter for the host to be patched"
$baseline = Get-Baseline -Name *Critical* -server $vc


#$vmhost = read-host "enter hostname that needs to be patched"
#write-host ""


$vmhosts = get-vmhost (get-content "C:\scripts\esxhosts.txt")


foreach ($vmhost in $vmhosts) {
if ($vmhost.ConnectionState -eq "connected") {
Set-VMHost -VMHost $vmhost -State Maintenance -Confirm:$false
while($vmhost.state -ne "Maintenance"){
sleep 5
#check what Vms are remaining on host
$vm_to_shutdown = $vmhost | get-vm -name *ntnx*
if (($vmhost | get-vm).count -eq "1")
#shutdown appliance vm
shutdown-vmguest -name $vm_to_shutdown -confirm:$false
}

$baseline | Attach-Baseline -entity $vmhost -Confirm:$false


Scan-Inventory -Entity $vmhost
sleep 30

$baseline | Remediate-Inventory -Entity $vmhost -Confirm:$false -ErrorAction SilentlyContinue

#start-sleep -s 300

get-vmhost -name $vmhost | set-vmhost -state Connected -Confirm:$false
while ($vmhost.state -ne "connected") {
sleep 5
}
start-vm -name $vm_to_shutdown -confirm:$false
 

 
0 Kudos
LucD
Leadership
Leadership

Is there a question there?


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

0 Kudos