Export/Private/Wait-ForVMShutdown.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function Global:Wait-ForVMShutdown {
    [CmdletBinding()]
    param(
        $ResourceGroupName,
        $VMName
    )
    process {        
        $VMShutdown = $false
        while (-not $VMShutdown) {
            Write-Verbose "Sleeping for 5 seconds, waiting for VM to shutdown..."
            Start-Sleep -Seconds 10  
            $statusCode = (Get-AzVM -ResourceGroupName $ResourceGroupName  -Name $VMName -Status).Statuses[1].Code
            $VMShutdown = ($statusCode -eq "PowerState/deallocated") -or ($statusCode -eq "PowerState/stopped")
        }        
    }    
}