我在 Azure 中有一个运行 Powershell Runbook 的自动化帐户。 当我通过门户触发它并手动指定输入时,它运行得非常好
我创建了一个具有相同输入设置的 Webhook。我从 CURL 中调用它,就像
curl -d '' https://800b2bec-b1ae-4fa1-ba30-8c7d32096828.webhook.ae.azure-automation.net/webhooks?[redactedtoken]
Webhook 在门户中显示为已成功触发,但作业失败且没有可见错误。
没有输出,即使我的 powershell 函数中的第一行是 Write-Output "Hello"
没有异常消息,根本没有日志。
知道如何获得有关可能出现问题的更多信息吗?
我已更新 Az 模块并在 Runbook 中启用了详细日志记录。
下面是完整的源代码,如果有帮助的话。
Param(
[string]$resourceGroup,
[string]$VMName,
[string]$method,
[string]$UAMI
)
Write-Output "Hello"
$automationAccount = "AlsAutomation"
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process | Out-Null
# Connect using a Managed Service Identity
try {
$AzureContext = (Connect-AzAccount -Identity).context
}
catch{
Write-Output "There is no system-assigned user identity. Aborting.";
exit
}
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
-DefaultProfile $AzureContext
if ($method -eq "SA")
{
Write-Output "Using system-assigned managed identity"
}
elseif ($method -eq "UA")
{
Write-Output "Using user-assigned managed identity"
# Connects using the Managed Service Identity of the named user-assigned managed identity
$identity = Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup `
-Name $UAMI -DefaultProfile $AzureContext
# validates assignment only, not perms
if ((Get-AzAutomationAccount -ResourceGroupName $resourceGroup `
-Name $automationAccount `
-DefaultProfile $AzureContext).Identity.UserAssignedIdentities.Values.PrincipalId.Contains($identity.PrincipalId))
{
$AzureContext = (Connect-AzAccount -Identity -AccountId $identity.ClientId).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
}
else {
Write-Output "Invalid or unassigned user-assigned managed identity"
exit
}
}
else {
Write-Output "Invalid method. Choose UA or SA."
exit
}
# Get current state of VM
$status = (Get-AzVM -ResourceGroupName $resourceGroup -Name $VMName `
-Status -DefaultProfile $AzureContext).Statuses[1].Code
Write-Output "`r`n Beginning VM status: $status `r`n"
# Start or stop VM based on current state
if($status -eq "Powerstate/deallocated")
{
Start-AzVM -Name $VMName -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext
}
elseif ($status -eq "Powerstate/running")
{
Stop-AzVM -Name $VMName -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext -Force
}
# Get new state of VM
$status = (Get-AzVM -ResourceGroupName $resourceGroup -Name $VMName -Status `
-DefaultProfile $AzureContext).Statuses[1].Code
Write-Output "`r`n Ending VM status: $status `r`n `r`n"
Write-Output "Account ID of current context: " $AzureContext.Account.Id
最佳答案
我们已经在本地环境中对其进行了测试,运行良好,以下陈述基于分析。
在我们的本地环境中,我们创建了一个使用不同 PowerShell 版本 7.1 和版本 5.1 运行的 Powershell Runbook。
- 使用上述共享脚本和 webhook URI,当我们尝试使用“Invoke-webRequest 方法”调用 Runbook(PowerShell 版本 7.1)时,它不断失败。
或者,我们尝试使用 Invoke-webRequest
方法调用 Runbook(PowerShell 版本 5.1),它工作正常。
我们建议您在 Runbook 中使用 Powershell 版本 5.1 而不是 7.1。
这是供引用的示例输出:
关于Azure 自动化作业手动运行良好,但在由 Webhook 触发时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71002575/