PowerShell do until or while exiting script
My script is exiting after the when the while loop is done. I have also
tried do until, but that exits as well. If I am going about this wrong let
me know.
#ADD VMWARE PSSNAPIN
Add-PSSnapin -Name "VMware.VimAutomation.Core"
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
[Reflection.Assembly]::LoadWithPartialName("vmware.vim")
$server_name = "VCENTER"
$vm_name = "VM_STAGING13"
$vc = Connect-VIServer -Server $server_name
$vm = Get-VM -Name $vm_name
#If the VM is not powered off then shut it down.
if ($vm.PowerState -notmatch "PoweredOff")
{
Shutdown-VMGuest -VM $vm.Name -Confirm:$false
}
#Sleep and check the power state of the VM.
do {
Start-Sleep -s 1
$vmpower = Get-VM -Name $vm_name | select PowerState
Write-Host "Sleeping 1 second...$vmpower"
}
while ($vmpower -notmatch "PoweredOff")
#If the VM indicates that it is off then update resources.
if ($vm.PowerState -match "PoweredOff")
{
Write-Host "Setting VM resources"
Set-VM -VM $vm.Name -MemoryMB "256" -NumCpu "1" -Confirm:$FALSE
Write-Host "Starting VM"
Start-VM -VM $vm.Name -Confirm:$FALSE
}
No comments:
Post a Comment