在 Azure Automation Runbook 中执行时出现 Set-AzureRmContext 错误

Set-AzureRmContext error when executed within an Azure Automation Runbook

更新:

似乎其他人也有同样的问题 reported 它。


我在从 Azure Automation Runbook 调用简单的 PowerShell 脚本时遇到问题。同一段代码 在 运行 本地 时可以完美运行。

我在 Azure Active Directory(托管在 Azure 德国云中)中添加了一个 Service Principal 密码凭据并授予它 contributor访问订阅(也托管在 Azure 德国云中)。

Azure 自动化服务托管在北欧,因为它目前在 Azure 德国云中不可用。

我尝试做的就是使用 Add-AzureRmAccount cmdlet 以上述主体登录我的订阅。之后,我尝试使用 Set-AzureRmContext 设置当前上下文并收到以下错误消息:

Set-AzureRmContext : Please provide a valid tenant or a valid subscription.
At line:26 char:1
+ Set-AzureRmContext -TenantId $TenantId -Su ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzureRmContext], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

这是我尝试 运行 的脚本(将配置留空):

$TenantId = ""
$ApplicationId = ""
$ClientSecret = ""
$SubscriptionId = ""

$secpasswd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($ApplicationId , $secpasswd)

Add-AzureRmAccount -ServicePrincipal -Environment 'AzureGermanCloud' -Credential $mycreds -TenantId $TenantId
Set-AzureRmContext -TenantId $TenantId -SubscriptionId $SubscriptionId

我也试过用Login-AzureRmAccount没成功。此外,我还能够使用 Get-AzureRmResourceGroup cmdlet 检索资源组,因此登录似乎有效。

所有 Azure 模块都更新到 最新 版本。


TLTR:

我的主要目标是使用 运行nbook 中的 New-AzureRmSqlDatabaseExport 启动 SQL 导出作业,但似乎上述错误导致 cmdlet 失败并显示以下内容留言:

New-AzureRmSqlDatabaseExport : Your Azure credentials have not been set up or have expired, please run 
Login-AzureRMAccount to set up your Azure credentials.
At line:77 char:18
+ ... rtRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $Resource 

几周前我遇到了同样的问题,有效的方法是首先使用以下方法登录到 Azure 帐户(我想你已经这样做了):

Login-AzureRmAccount

然后从 Azure 获取订阅 ID 并使用 select 使用 ID 而不是名称的订阅,如下所示:

Select-AzureRmSubscription -SubscriptionId {insert-subscription-id}

当您登录您的Azure 帐户时,您可以使用指定的订阅ID。您可以尝试以下脚本。

$subscriptionId=""
$tenantid=""
$clientid=""
$password=""
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientid, $userPassword
Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionId $subscriptionId -Credential $userCredential -Environment 'AzureGermanCloud'

下面是对我有用的代码(常规直流区域)。如果它不起作用,请转到 Automation Account >> Modules >> Update Azure Modules .

$ClientSecret = ""
$ApplicationId = ""
$SubscriptionId = ""

#New PSCredential Object
$secpasswd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($ApplicationId , $secpasswd)

#Login to subscription
Login-AzureRmAccount -Credential $mycreds -SubscriptionId $SubscriptionId

#Export Database
New-AzureRmSqlDatabaseExport -ResourceGroupName "<RG>" -ServerName "<SQLSERVERNAME>" -DatabaseName "<DATABASENAME>" -StorageKeyType "StorageAccessKey" -StorageKey "<STRKEY>" -StorageUri "<URITOFILE>" -AdministratorLogin "<DBLOGIN>" -AdministratorLoginPassword "<DBPASS>"

更新

也许 运行 一个 运行 As Account 可以解决这个问题。通过导航到 Azure Automation Account >> Account Settings >> 运行 As Accounts[=25 创建一个=].这是一个示例代码。

# Authenticate to Azure with service principal and certificate, and set subscription
$connectionAssetName = "AzureRunAsConnection"
$conn = Get-AutomationConnection -Name $ConnectionAssetName

Add-AzureRmAccount -ServicePrincipal -Tenant $conn.TenantID -ApplicationId $conn.ApplicationId -CertificateThumbprint $conn.CertificateThumbprint -ErrorAction Stop | Write-Verbose
Set-AzureRmContext -SubscriptionId $conn.SubscriptionId -ErrorAction Stop | Write-Verbose

这似乎是一个 known issue,我找不到解决办法。但是有两个解决方法:

  1. 使用 Hybrid Runnbook Worker (mentioned by Walter - MSFT)
  2. 使用 RunAsAccountcertificate 凭据 ()

指定 -Environment 参数很重要。否则我会得到以下异常:

Login-AzureRmAccount : AADSTS90038: Confidential Client is not supported in Cross Cloud request.

这是我用于从北欧托管的 Azure Runbook 登录 AzureGermanCloud (MCD) 的代码:

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

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