【问题标题】:Azure runbook to start/stop a vm says "Completed" but nothing happens用于启动/停止 vm 的 Azure Runbook 显示“已完成”,但没有任何反应
【发布时间】:2016-07-28 16:11:42
【问题描述】:

我对 Azure 还很陌生,但我正在努力解决一些看似简单的问题。我已经阅读了大量资源,这些资源表明这应该很容易,但我根本无法在新门户 (ARM) 中获得运行手册来启动特定的 VM。它运行并显示“已完成”,但没有任何反应。

这是脚本:

在 Jack Zeng 的评论之后更新,遗憾的是变化不大。

workflow StartDEVTC1
{
$VerbosePreference = "Continue"
$ErrorActionPreference = "Stop"
$WarnPreference = "Continue"

Write-Output "Getting credential..."
$cred = Get-AutomationPSCredential -Name 'AutomationPowershellCred1'
Write-Output "Adding account..."
Add-AzureRmAccount -Credential $cred

Write-Output "Getting VM..."
$VM = Get-AzureRmVM -ResourceGroupName "myResourceGroup" -Name "DEVTC1" -Status

$vmName = $VM.Name
$vmRG = $VM.ResourceGroupName
Write-Output "Checking state of $vmName from $vmRG..."
$VMDetail = $VM | Select-Object -ExpandProperty StatusesText | convertfrom-json
$vmPowerstate = $VMDetail[1].Code

if($vmPowerstate -like "PowerState/deallocated")
{  
    Write-Output "$vmName powerstate is $vmPowerstate, starting VM..."
    $res = $VM | Start-AzureRmVM
    if (($res.StatusCode) -ne 'OK')
    {
        Write-Error "Could not start VM."
    }
}
else
{
    Write-Output "$vmName powerstate is $vmPowerstate, not starting VM."
}

Write-Output "END."
}

如您所见,我尝试添加一些日志记录,但在测试窗格中看不到这些。在我的 PC 上的 Powershell 中运行关键步骤工作正常,那么这里发生了什么?

已使用我的 Azure 帐户的详细信息重新定义了凭据(我是该帐户的所有者)。

不言而喻,但我尝试了一些更简单的方法来调用 Start-AzureRmVm,但没有任何效果。

提前感谢和赞赏!

【问题讨论】:

  • 请将Write-Progress 更改为Write-Output,看看你会得到什么输出。 Write-Progress cmdlet 在 Runbook 中无效,因为它是供交互式用户使用的。顺便说一句,对于Start-AzureRmVM,不再支持-Force 参数。我已经尝试过你的代码,它在我的最后完美运行,没有 -Force 并将 Write-Progress 替换为 Write-Output
  • 谢谢杰克,但即使有你的建议,我也不会高兴。在“测试”窗格中,我看不到任何输出,也看不到任何错误。我只能看到“已完成”和未更改的虚拟机。
  • 这很奇怪。它对我来说很完美。我在这里没有看到任何问题。

标签: powershell azure automation runbook


【解决方案1】:

好的,我明白了,答案非常简单。

您不能使用 workflow 块,除非您的 Runbook 是作为“PowerShell 工作流”Runbook 创建的。就我而言,它只是一个“PowerShell”运行手册,我猜它只是忽略了工作流代码块。

将运行手册重新创建为“PowerShell 工作流程”运行手册允许输出日志记录,这样我就可以看到正在发生的事情,并且虚拟机按预期启动(尽管花了 真的 很长时间)。

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2017-03-18
    • 1970-01-01
    • 2014-06-28
    相关资源
    最近更新 更多