Testing Azure Automaton Runbooks with Error: WriteErrorException

Testing Azure Automaton Runbooks with Error: WriteErrorException

因此,我已将以下代码输入到 运行 订阅免费试用 Azure 的自动化帐户内的书中。

这本 运行 书的目的是在周末自动关闭我的 VM,并在星期一早上自动启动该 VM。调度创建没有问题。

参数标记为 "optional",当所有外部参数字段都留空时,它会 select 正确的虚拟机 (ITAMTradingVPS)。也没有问题。

但是,当我进行 "Test" 以确保所有编码正确并且脚本执行其预期的操作时,为什么会出现以下错误以及我需要做什么解决这个问题?

运行 图书是从自动化帐户 Blade 创建的 "Powershell Runbook"

如果您需要任何进一步的信息,或者需要我澄清任何事情,请随时发表评论。

谢谢

ITAMTradingVPS failed to stop. Error was:
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException


Status was 
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

这是我的自动化流程代码

param (
  
    [Parameter(Mandatory=$false)] 
    [String] $VMName ,
 
    [Parameter(Mandatory=$false)] 
    [String] $ResourceGroupName
)
 
$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
 
    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
 
 
# If there is a specific resource group, then get all VMs in the resource group,
# otherwise get all VMs in the subscription.
if ($ResourceGroupName -And $VMName) 
{ 
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
}
elseif ($ResourceGroupName)
{
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName
 
}
else 
{ 
    $VMs = Get-AzureRmVM
}
# Start each of the VMs
# Stop each of the VMs
foreach ($VM in $VMs)
{
    $StopRtn = $VM | Stop-AzureRmVM -Force -ErrorAction Continue
 
    if ($StopRtn.Status -ne 'succeeded')
    {
        # The VM failed to stop, so send notice
        Write-Output ($VM.Name + " failed to stop")
        Write-Error ($VM.Name + " failed to stop. Error was:") -ErrorAction Continue
        Write-Error ("Status was "+ $StopRtn.Status) -ErrorAction Continue
        Write-Error (ConvertTo-Json $StopRtn.Error) -ErrorAction Continue
    }
    else
    {
        # The VM stopped, so send notice
        Write-Output ($VM.Name + " has been stopped")
    }
}

我在我的本地机器 powershell 上测试了你的脚本,工作正常。但是 运行 它在 Azure 运行book 中,得到与你相同的错误消息。

因为$StopRtn输出是这样的:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK          

所以我们应该这样修改powershell脚本:

param (

    [Parameter(Mandatory=$false)] 
    [String] $VMName ,

    [Parameter(Mandatory=$false)] 
    [String] $ResourceGroupName
)

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}


# If there is a specific resource group, then get all VMs in the resource group,
# otherwise get all VMs in the subscription.
if ($ResourceGroupName -And $VMName) 
{ 
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName
}
elseif ($ResourceGroupName)
{
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName

}
else 
{ 
    $VMs = Get-AzureRmVM
}

$vms
# Start each of the VMs
# Stop each of the VMs
foreach ($VM in $VMs)
{
    $StopRtn = $VM | Stop-AzureRmVM -Force -ErrorAction Continue
    $StopRtn
    Write-Output " this is $StopRtn "

    if ($StopRtn.IsSuccessStatusCode -eq 'True')
    {
        # The VM stopped, so send notice
        Write-Output ($VM.Name + " has been stopped")

    }
    else
    {
        # The VM failed to stop, so send notice
        Write-Output ($VM.Name + " failed to stopped")
    }
}

希望对您有所帮助。