因此,我在一个自动化帐户中的一个免费试用Azure订阅中输入了下面的代码。
这个Run Book的目的是在周末自动关闭我的VM,并在周一早上自动启动这个VM。在计划创建中没有任何问题。
参数被标记为“可选”,当所有外部参数字段都为空时,它确实选择了正确的虚拟机(ITAMTradingVPS)。那里也没问题。
然而,当我进行“测试”以确保所有的编码都是正确的,并且脚本做了它应该做的事情时,为什么我会得到以下错误,我需要做什么来修复这个错误呢?
Run Book是使用"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")
}
}
发布于 2017-12-22 07:21:31
我在本地机器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")
}
}希望这能有所帮助。
https://stackoverflow.com/questions/47930384
复制相似问题