Azure power shell虚拟机创建问题

时间:2016-08-26 04:34:19

标签: powershell azure virtual-machine azure-storage-blobs

我是Azure power shell世界的新手。我正在尝试创建电源shell脚本以自动创建VM。我的所有脚本运行良好,VM也正在创建,但它在最后一行被绞死了。因此,即使虚拟机被创建,我的电源shell脚本也会继续运行。请告诉我如何解决这个问题。

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.'
}

2 个答案:

答案 0 :(得分:0)

您可以采取其他方式,以验证VM的状态

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

并查看:https://4sysops.com/archives/how-to-create-an-azure-vm-with-powershell/

答案 1 :(得分:0)

$ result的返回类型将是一个psobject。成功创建VM后,输出将采用以下格式

  

$ result [0] =“配置成功”

     

$ result [1] =“VM running”

所以,试试这个 -

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.'
}