tdubb123
Expert
Expert

shutdown-vmguest and set cpu and mem

will this work?

have not had a chance to test

$vms = get-vm -name (get-content vms.txt)

foreach ($vm in $vms) {

get-VM -name $vm | Shutdown-VMGuest -Confirm:$false

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

   Write-Host "Shutting Down" $VMName

   Shutdown-VMGuest -VM $VM

   #Wait for Shutdown to complete

   do {

      #Wait 5 seconds

      Start-Sleep -s 5

      $status = $VM.PowerState

   }until($status -eq "PoweredOff")

}

$vm | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False

$vm  | Start-VM

}

0 Kudos
8 Replies
satheeshsatz
Enthusiast
Enthusiast

Below will work

$vms = get-content C:\temp\vms.txt

foreach ($vm in $vms) {

get-VM -name $vm | Shutdown-VMGuest -Confirm:$false

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

   Write-Host "Shutting Down" $VMName

   #Wait for Shutdown to complete

   do {

      #Wait 5 seconds

      Start-Sleep -s 5

      $status = $VM.PowerState

   }until($status -eq "PoweredOff")

}

get-VM -name $vm | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False

get-VM -name $vm  | Start-VM

}

Regards, Satheesh
0 Kudos
LucD
Leadership
Leadership

Try like this, it avoids a bit of overhead.

$vms = Get-VM -name (Get-Content vms.txt)

foreach ($vm in $vms) {

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

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

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

  sleep 5

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

   }

   }

   Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False

   Start-VM -VM $vm -Confirm:$false

}


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

0 Kudos
tdubb123
Expert
Expert

hi I am trying to add a piece to check for vmware tools status is ok before moving on to next VM.

but script is getting hung

$vms = Get-VM -name (Get-Content vms.txt)

foreach ($vm in $vms) {

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

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

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

sleep 5

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

$toolsstatus = $vm.ExtensionData.guest.ToolsStatus

}

}

Set-VM -VM $vm –MemoryGB 2 –NumCpu 1 –Confirm:$False

Start-VM -VM $vm -Confirm:$false

while ($toolsstatus -ne "toolsok"){

sleep 20

}

}

any idea?

0 Kudos
tdubb123
Expert
Expert

unableto verify if this will work.  Can you let me know. thnaks

$vms = Get-VM -name (Get-Content vms.txt)

foreach ($vm in $vms) {

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

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

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

sleep 5

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

}

}

Set-VM -VM $vm –MemoryGB 2 –NumCpu 1 –Confirm:$False

Start-VM -VM $vm -Confirm:$false

while ($toolsstatus -ne "toolsok"){

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

$toolsstatus = $vm.ExtensionData.guest.ToolsStatus

}

}

0 Kudos
LucD
Leadership
Leadership

That last version should work, in the previous one you forgot to refresh the $toolsstatus content.


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

0 Kudos
tdubb123
Expert
Expert

hi

looks like its shutting the next vm down before the tools status is ok on the previous vm. I used the wait-tools cmdlet and seems to be working better

$vms = Get-VM -name (Get-Content vms.txt)| sort

foreach ($vm in $vms) {

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

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

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

sleep 5

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

}

}

Set-VM -VM $vm –MemoryGB 2 –NumCpu 1 –Confirm:$False

Start-VM -VM $vm -Confirm:$false

Start-Sleep 3

Wait-Tools -vm $vm

}

0 Kudos
tdubb123
Expert
Expert

trying to do this all asynchronously

havent tested at a bigger scale but only 5 vms and seem to work.

Thoughts or suggestions?

$vms = Get-VM -name (Get-Content "c:\scripts\vms.txt")| sort

$new_VM_mem = read-host "Enter the new Memory in GB"

$new_VM_cpu = read-host "Enter the new # of cpus"

if($vms.PowerState -eq 'PoweredOn'){

$vms | Shutdown-VMGuest -Confirm:$False

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

sleep 5

$vms = Get-VM -Name $vms.Name

}

}

Set-VM -VM $vms –MemoryGB $new_VM_mem –NumCpu $new_VM_cpu –Confirm:$False

Start-VM -VM $vms -Confirm:$false

0 Kudos
LucD
Leadership
Leadership

Not sure I understand the question.
What exactly are you asking?

That last script will update the Cpu and Memory of all VMs in sequence.


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

0 Kudos