Azure Runbook 脚本无法使用系统分配的身份登录

Azure Runbook script can't login with system assigned identity

我正在尝试设置 Powershell Runbook 以访问 Azure 资源。

我正在按照 this page 上的说明进行操作。

我已经为自动化帐户分配了一个系统身份,示例脚本根本不起作用。

# Sign in to your Azure subscription
$sub = Get-AzSubscription -ErrorAction SilentlyContinue
if(-not($sub))
{
     Connect-AzAccount
}

输出:

Run Connect-AzAccount to login.
Port 8400 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Port 8401 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
<snip>
Port 8998 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Port 8999 is taken with exception 'A socket operation encountered a dead network'; trying to connect to the next port.
Unable to acquire token for tenant 'organizations' with error 'Cannot find an open port.'
Cannot find an open port.

我尝试为身份添加贡献者和所有者角色,但没有帮助。
我不确定我错过了什么,帮忙?

我正在使用 terraform 来建立基础设施,所以我可能有一个 terraform 正在设置的设置。

您使用的代码片段用于身份验证以启用身份。

您需要使用该页面下方的代码片段:

https://docs.microsoft.com/en-us/azure/automation/enable-managed-identity-for-automation#authenticate-access-with-system-assigned-managed-identity

您会注意到它使用了 Connect-AzAccount -Identity

# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process

# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext