SOLVED

While not entering loop

New Contributor

Hi community,

i want to shutdown a windows server to take a vmware snapshot.

So I have to check if the vm is powered down on esx.

 

 

 

# connect-VIServer -Server vm-vcenter001.contoso.com

$computer = "vm-sql"

Stop-Computer $computer -Force -Confirm:$false

$vm = Get-VM $computer

$vmstate = $vm.PowerState

$vmstate


while ($vmstate -eq 'PoweredOff') {

        Start-Sleep -Seconds 5
        Write-Host "$computer is $($vm.PowerState)"
        $vmstate = (get-vm $vm).PowerState
    }
Write-Host "VM down"

 

 

 

 The problem is that never entering the loop. All properties and variables are correct.

 

After the script this condition is true. So it schuld be fine.

 

 

 

$vmstate -eq "PoweredOff"

 

 

 

I think it has something to do, that the condition ist with a string?!

 

Any help will be wonderfull.

 

2 Replies
best response confirmed by Skyscraper19 (New Contributor)
Solution

@Skyscraper19 

 

Wouldn't you want line 14 to be:

 

 

while ($vmstate -ne 'PoweredOff') {

 

 

Cheers,

Lain

@LainRobertson You are right