简体   繁体   English

Azure Power Shell虚拟机创建问题

[英]Azure power shell Virtual machine creation issue

I am new to Azure power shell world. 我是Azure Power Shell世界的新手。 I am trying to create power shell script to automate VM creation. 我正在尝试创建Power Shell脚本来自动创建VM。 All my script running well, VM is getting created as well, but it's getting hanged on its last line. 我所有的脚本运行良好,VM也正在创建中,但是挂在了最后一行。 So even though the virtual machine gets created my power shell script is keep on running. 因此,即使创建了虚拟机,我的Power Shell脚本仍继续运行。 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 您可以采取另一种方法来验证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/ 并查看: https : //4sysops.com/archives/how-to-create-an-azure-vm-with-powershell/

return type of $result will be a psobject. $ result的返回类型将是psobject。 After successful VM creation the output will be in below format 成功创建虚拟机后,输出将采用以下格式

$result[0] = "Provisioning succeeded" $ result [0] =“预配置成功”

$result[1] = "VM running" $ result [1] =“虚拟机正在运行”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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