启动/停止虚拟机的Azure Runbook提示“已完成”,但没有任何反应

Gla*_*oon 2 powershell automation azure runbook

我对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."
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我尝试添加一些日志记录,但是在“测试窗格”中看不到这些日志记录。在PC上的Powershell中运行关键步骤就可以了,那么这是怎么回事?

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

不用说,但是我尝试了一些简单得多的方法来调用Start-AzureRmVm,但是没有任何效果。

预先感谢和赞赏!

Gla*_*oon 6

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

workflow除非您将运行手册创建为“ PowerShell Workflow”运行手册,否则不能使用块。在我的情况下,它只是一个“ PowerShell”运行手册,我想只是在忽略工作流代码块。

将运行手册重新创建为“ PowerShell Workflow”运行手册可以输出日志,因此我可以看到发生了什么,虚拟机按预期启动了(尽管花费了长时间)。