Azure Automation start script when vm finish provisioning


If you want to make end to end automation in Azure. You need to attach some sort of script after the vm finished provisioning.

There are different status while you creating vm. You dont want to send a script when the server is provisioning.

You can use the following PowerShell script to detect the vm is finished provisioning, and start you script afterwards.

 1
2
3
4
5
6
7
8
9
10
11
12
13
$vmname=''
InLineScript
{
while ((Get-AzureVM -ServiceName $ServiceName -Name $using:vmname).InstanceStatus -ne 'ReadyRole')
{
Get-AzureVM -ServiceName $ServiceName -Name $using:vmname
Write-output 'Try again in 45 seconds'
Start-Sleep -Seconds 45
}
Start-Sleep -Seconds 1
Write-output 'Server status is ReadyRole now.'
}
#Start your custom script from here.