Export/Private/Wait-ForInstanceAvailability.ps1

# Will be called in VM
function Global:Wait-ForInstanceAvailability {
    [CmdletBinding()]
    <#
    .SYNOPSIS
        This CmdLet will wait until the ScaleSet-Instance of this VM is in ProvisionigState 'Succeeded'
    .DESCRIPTION
        ...
    #>

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]
        $ScaleSetName
    )
    process {
        Write-Verbose "Checking Instance-availability..."        
        $params = @{
            VMScaleSetName    = $ScaleSetName
            ResourceGroupName = $ResourceGroupName
        }
        while ((Get-AzVmssVM @params | Where-Object {$_.OsProfile.ComputerName -eq $env:COMPUTERNAME}).ProvisioningState -ne "Succeeded"){        
            Write-Verbose "Waiting for Instance-availability (checking every 5 seconds)"
            Start-Sleep -Seconds 5
        }
        Write-Verbose "Instance is available."
    }
}