在Azure自动化Runbook中执行时设置AzureRmContext错误

Mar*_*ndl 7 powershell azure azure-automation azure-mcd

更新:

好像其他人有同样的问题并报告了它.


从Azure自动化Runbook调用时,我遇到了一个简单的PowerShell脚本问题.在本地运行它时,同一段代码完美无瑕.

我已使用密码凭据在Azure Active Directory(托管在Azure德语云中)中添加了服务主体,并授予其参与者对订阅的访问权限(也在Azure德语云中托管).

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

我尝试做的就是使用Add-AzureRmAccountcmdlet 使用上面提到的主体登录我的订阅.之后,我尝试使用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
Run Code Online (Sandbox Code Playgroud)

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

$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
Run Code Online (Sandbox Code Playgroud)

我也尝试使用Login-AzureRmAccount没有成功.此外,我可以使用Get-AzureRmResourceGroupcmdlet检索资源组,以便登录似乎工作.

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


TLTR:

我的主要目标是使用New-AzureRmSqlDatabaseExport来自runnbook 的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 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ndl 2

看来这是一个已知问题,我无法找到解决方案。但有两种解决方法

  1. 使用混合 Runnbook WorkerWalter - MSFT 提到
  2. 使用带有证书凭据的RunAsAccount由 Bruno Faria 提到

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

Login-AzureRmAccount:AADSTS90038:跨云请求不支持机密客户端。

以下是我用来从北欧托管的 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
Run Code Online (Sandbox Code Playgroud)