首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用错误测试Azure自动机运行库: WriteErrorException

用错误测试Azure自动机运行库: WriteErrorException
EN

Stack Overflow用户
提问于 2017-12-21 18:02:36
回答 1查看 2.2K关注 0票数 0

因此,我在一个自动化帐户中的一个免费试用Azure订阅中输入了下面的代码。

这个Run Book的目的是在周末自动关闭我的VM,并在周一早上自动启动这个VM。在计划创建中没有任何问题。

参数被标记为“可选”,当所有外部参数字段都为空时,它确实选择了正确的虚拟机(ITAMTradingVPS)。那里也没问题。

然而,当我进行“测试”以确保所有的编码都是正确的,并且脚本做了它应该做的事情时,为什么我会得到以下错误,我需要做什么来修复这个错误呢?

Run Book是使用"Powershell运行簿“从自动化帐户刀片创建的

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

谢谢

代码语言:javascript
复制
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

这是我对自动化过程的代码。

代码语言:javascript
复制
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")
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-22 07:21:31

我在本地机器powershell上测试您的脚本,运行良好。但是在Azure runbook中运行它,得到与您相同的错误消息。

因为$StopRtn输出是这样的:

代码语言:javascript
复制
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK          

因此,我们应该像这样修改powershell脚本:

代码语言:javascript
复制
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")
    }
}

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47930384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档