简体   繁体   中英

Azure power shell Virtual machine creation issue

I am new to Azure power shell world. I am trying to create power shell script to automate VM creation. All my script running well, VM is getting created as well, but it's getting hanged on its last line. So even though the virtual machine gets created my power shell script is keep on running. Please advise me how to address this issue.

Write-Verbose 'Creating VM...'  
$result = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm
if($result.Status -eq 'Succeeded') {  
 Write-Verbose $result.Status
 Write-Verbose ('VM named ''{0}'' is now ready, you can connect using  username: {1} and password: {2}' -f $vmName, $adminUsername, $adminPassword)
} 
  else {
Write-Error 'Virtual machine was not created successfully.'
}

you can take another way, to verify the state of the VM

New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm
if((Get-AzureRmVM -Name $vmName).Status -eq "ReadyRole"){
  #Do something awesome here :)
}

and have a look at: https://4sysops.com/archives/how-to-create-an-azure-vm-with-powershell/

return type of $result will be a psobject. After successful VM creation the output will be in below format

$result[0] = "Provisioning succeeded"

$result[1] = "VM running"

So, try this-

if($result.ProvisioningState -eq "Succeeded")
{  
    Write-Verbose ('VM named ''{0}'' is now ready, you can connect using    username: {1} and password: {2}' -f $VMName, $username, $password)
} 
else
{
    Write-Error 'Virtual machine was not created successfully.'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM