所以我在免费试用Azure订阅下的自动化帐户中输入了以下代码到Run Book中。
本Run Book的目的是在周末自动关闭我的VM,并在周一早上自动启动这个VM。计划创建中没有问题。
参数标记为“可选”,当所有外部参数字段留空时,它会选择正确的虚拟机(ITAMTradingVPS)。那里也没问题。
但是,当我进行“测试”以确保所有编码都正确并且脚本执行了应该执行的操作时,为什么我会收到以下错误以及我需要做些什么才能解决此问题?
Run Book是使用“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")
}
}
答案 0 :(得分:0)
我在本地计算机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")
}
}
希望这有帮助。