Azure PS 自动化提供的订阅 "xxxx" 不存在

Azure PS Automation Provided subscription "xxxx" does not exists

我希望 select 在 Azure 自动化 PS 作业中为我的服务原则提供订阅。 运行 以下代码在本地工作正常,但在自动化作业中,我只收到以下错误

Provided subscription xxxx-xxxx-xxxx-xxx-xxxx does not exist.

订阅确实存在,当我在本地登录时,服务主体可以访问它。

$id = "someid"
$pass = "somepass"

$securePass = $pass | ConvertTo-SecureString -AsPlainText -Force

$cred = new-object -TypeName System.Management.Automation.PsCredential -ArgumentList $id, $securePass

$tenantId = "someID"

Add-AzureRmAccount -Credential $cred -TenantId $tenantId -ServicePrincipal

Select-AzureRmSubscription -SubscriptionId "someID"

要使用 Azure 自动化,您应该创建启用 AzureRunAsConnection 的自动化帐户。然后从脚本中像这样使用它

$connectionName = "AzureRunAsConnection"
try {
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Connect-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
    }
}

希望对您有所帮助

几天后,我终于弄明白了这个问题。

已报告此问题 here

这是由于 Add-AzureRmAccount cmdlet 与 Service Principal 相关的问题。

Hariharan

所述,有解决此问题的方法
$connectionAssetName = "AzureRunAsConnection"
$conn = Get-AutomationConnection -Name $ConnectionAssetName

Login-AzureRmAccount `
    -ServicePrincipal `
    -CertificateThumbprint $conn.CertificateThumbprint `
    -ApplicationId $conn.ApplicationId `
    -TenantId $conn.TenantID `
    -Environment AzureGermanCloud

参考这个