[英]Testing Azure Automaton Runbooks with Error: WriteErrorException
因此,我已經在免費試用版Azure訂閱下的自動化帳戶內的運行簿中輸入了以下代碼。
該運行手冊的目的是在周末自動關閉我的VM,並在星期一早上自動啟動該VM。 計划創建中沒有問題。
這些參數被標記為“可選”,並且當所有外部參數字段均保留為空白時,它將選擇正確的虛擬機(ITAMTradingVPS) 。 那里也沒有問題。
但是,當我進行“測試”以確保所有編碼正確並且腳本執行了應做的工作時,為什么會出現以下錯誤,我需要怎么做才能解決此問題?
運行手冊是通過自動化帳戶刀片中的“ Powershell運行手冊”創建的
如果您需要任何進一步的信息,或者需要我澄清任何事情,請隨時發表評論。
謝謝
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 Runbook中運行它,會得到與您相同的錯誤消息。
因為$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")
}
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.