Link to home
Start Free TrialLog in
Avatar of K B
K BFlag for United States of America

asked on

POWERSHELL: do / until, should continue even when there is an error

Effectively, I am waiting till the VMs are in a state where my script can continue to action upon them.  However, the VMs are not quite built by the time this loop looks for them (in other words, there is NO status yet).

How do I force the script to keep checking "until" the statuses are obtained for the VMs (thus ending the DO/UNTIL)

Thank you!

function WaitforAllRunning($vmName)
        {
        do  {
            Start-Sleep -milliseconds 100
            $vmStatuses  = Get-AzureRmVM -VM $vmName -ResourceGroupName $rgName -Status -ErrorAction SilentlyContinue | select -ExpandProperty Statuses | Select -ExpandProperty DisplayStatus
            $vmStatus0   = $VMStatuses[0]
            $vmStatus1   = $VMStatuses[1]
            $vmStatusC   = Get-AzureRmVM -VM $vmName -ResourceGroupName $rgName -Status -ErrorAction SilentlyContinue | select -ExpandProperty Statuses | Select -ExpandProperty Code
            $vmCode      = $vmStatusC[0]
            }
        
        until ({$vmStatus0 -eq 'Provisioning succeeded'} -and {$vmStatus1 -eq 'VM running'} -and {$vmCode = 'ProvisioningState/succeeded'})
        } 

Open in new window


Here is the error:

User generated image
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of K B

ASKER

I cannot tell you how appreciative I am of your brilliance!

Thank you!  I have spent days trying to figure it out on my own.

I will also study what you have so graciously provided and attempt to learn from it.
Avatar of oBdA
oBdA

In case you haven't found it yet yourself - just noticed that I missed something: In line 13, the test for $vmCode should of course be "-eq" instead of "=":
    } Until (($vmStatus0 -eq 'Provisioning succeeded') -and ($vmStatus1 -eq 'VM running') -and ($vmCode -eq 'ProvisioningState/succeeded'))

Open in new window

Avatar of K B

ASKER

Thank you.  Made the adjustment.. it is working well.