VMware Cloud Community
tdubb123
Expert
Expert

update vmware tools check powerstate vm first

I need to update vmware tools on a bunch of vms from a txt file. But I also need to check the powerstate of the vm and then power them on before the update.

So trying to work with something like this

connect-VIserver vc

$VMs = Get-VM -Name (Get-Content -Path "C:\vms2.txt")

foreach ($VM in $VMs) {

if ($VM.PowerState -eq "PoweredOff") {

Start-VM -VM $VM -runasync -confirm:$false

start-sleep 30

}

update-tools -VM $VM -NoReboot -Runasync

}

how can i refine this or maybe if the sleep timer is too short to all the vms to start up?

Thank you

also I need to check for susp[ended state VMs and wake them up as well

0 Kudos
4 Replies
sneddo
Hot Shot
Hot Shot

how can i refine this or maybe if the sleep timer is too short to all the vms to start up?


Simplest solution would be to put a while loop, checking the powerState.


e.g.

While (@(Get-VM -Name (Get-Content -Path "C:\vms2.txt") | Where {$_.PowerState -ne "PoweredOn" }).Count -gt 0)

{

     start-sleep 30

}


also I need to check for suspended state VMs and wake them up as well



Either use not equal PoweredOn, or just add another clause for PowerState = Suspended.

0 Kudos
tdubb123
Expert
Expert

While (@(Get-VM -Name (Get-Content -Path "C:\vms2.txt") | Where {$_.PowerState -ne "PoweredOn" }).Count -gt 0)

{

     Start-VM -VM $VM -runasync -confirm:$false

     start-sleep 30

}

update-tools -VM $VM -NoReboot -Runasync

0 Kudos
tdubb123
Expert
Expert

I tried running this

While (@(Get-VM -Name (Get-Content -Path "C:\vms2.txt") | Where {$_.PowerState -ne "PoweredOn" }).Count -gt 0)

{

     Start-VM -VM $VM -runasync -confirm:$false

     start-sleep 30

}


but there was a vm already powered on and it was still trying to power it on

0 Kudos
LucD
Leadership
Leadership

You better do the power on and the wait in separate loops.

$vmNames = Get-Content -Path 'C:\vms2.txt'

$vms = Get-VM -Name $vmNames

# power on the VMs that are not powered on

$vms | where($_.PowerState -ne 'PoweredOn'){

    Start-VM -VM $_

}

# Wait till they are all powered on

While (@(Get-VM -Name $vmNames | Where {$_.PowerState -ne "PoweredOn" }).Count -gt 0)

{

     start-sleep 30

}


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

0 Kudos