Azure PS Automation提供的订阅“ xxxx”不存在

时间:2018-08-22 10:29:27

标签: azure powershell azure-automation

我希望为Azure自动化PS作业中的服务原则选择可用的订阅。在本地运行以下代码可以正常工作,但是在自动化作业中,我只会遇到以下错误

  

提供的订阅xxxx-xxxx-xxxx-xxx-xxxx不存在。

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

$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"

2 个答案:

答案 0 :(得分:0)

要使用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
    }
}

希望这会有所帮助

答案 1 :(得分:0)

最后,几天后,我发现了这个问题。

此问题已报告here

这是由于与带有服务主体 Add-AzureRmAccount cmdlet有关的问题。

有一种解决方法,如 Hariharan

所述
$connectionAssetName = "AzureRunAsConnection"
$conn = Get-AutomationConnection -Name $ConnectionAssetName

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

请参阅此S.O