【问题标题】:Azure power shell Virtual machine creation issueAzure powershell 虚拟机创建问题
【发布时间】:2017-01-02 15:46:43
【问题描述】:

我是 Azure power shell 世界的新手。我正在尝试创建 power shell 脚本来自动创建 VM。我的所有脚本运行良好,VM 也正在创建,但它被挂在最后一行。因此,即使创建了虚拟机,我的 power 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.'
}

【问题讨论】:

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


【解决方案1】:

你可以采取另一种方式,验证虚拟机的状态

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/

【讨论】:

  • 您为什么期望使用 Get-azureVM 命令获得 RM vm 状态?为什么是服务名称参数??
  • 你确定吗?应该是 get-azurermvm
  • (Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName).StatusCode 这给出了结果“OK”但是 (Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName).Statuses 没有任何结果
  • @Indrajit Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName 结果是什么?
  • 一个观察。你可能会发现我已经创建了在 power shell 中设计的 Win 窗体。因此,当我运行脚本时,win 表单似乎接受输入并根据输入运行脚本。现在,当我在没有任何 GUI 的情况下运行脚本时(我正在传递我的硬编码值),它并没有被挂起,而是在我使用 GUI 运行时。
【解决方案2】:

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

$result[0] = "配置成功"

$result[1] = "虚拟机正在运行"

所以,试试这个-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-30
    • 2021-01-02
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    相关资源
    最近更新 更多