【问题标题】:Set-AzureRmContext error when executed within an Azure Automation Runbook在 Azure 自动化 Runbook 中执行时出现 Set-AzureRmContext 错误
【发布时间】:2018-03-07 18:29:26
【问题描述】:

更新:

好像其他人也有同样的问题,reported 它。


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

我在 Azure Active Directory(托管在 Azure German Cloud 中)中添加了一个服务主体,并带有密码凭据,并授予它贡献者对订阅的访问权限(也托管在Azure 德国云)。

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

我要做的就是使用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:

我的主要目标是使用 runnbook 中的 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 

【问题讨论】:

  • 如果登录有效,为什么需要set-azurermcontext?只需将-SubscriptionId 添加到add-azurermaccount
  • @4c74356b41 因为New-AzureRmSqlDatabaseExport 仍然会抛出该异常。我试图提供一个最小的例子来重现我的问题,我认为Set-AzureRmContext 不起作用的答案将帮助我实现我的主要目标。
  • 为什么还要这样做?
  • @4c74356b41 如果我知道我不会问这个问题。我已经试过了。此外,当我尝试使用带有-SubscriptionId-TenantId 参数的Get-AzureRmSubscription 获取订阅时,我收到消息Subscription xxx was not found in tenant
  • 租户 ID 或订阅 ID 错误?

标签: powershell azure azure-automation azure-mcd


【解决方案1】:

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

Login-AzureRmAccount

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

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

【讨论】:

  • 谢谢 Clive,我会试一试并回复你
  • 不客气,以防万一您也可以尝试执行此操作:“Add-AzureAccount”登录到您的 Azure 帐户并添加您的帐户。
  • 感谢您的提示,但我怀疑它是否会有所帮助,因为我使用的是 Azure 资源管理器 (RM) cmdlet。
  • 不幸的是,我收到错误消息:Select-AzureRmSubscription : Please provide a valid tenant or a valid subscription
  • 我尝试将权限设置为所有者,但收到相同的错误(脚本也在本地工作)。 New-AzureRmSqlDatabaseExport 不采用任何上下文参数,因此您的第二个想法对我没有帮助。但是非常感谢您的努力。
【解决方案2】:

当您登录您的 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'

【讨论】:

  • 抱歉沃尔特我迟到的回复。不幸的是,这并没有改变任何东西。我还在最后一行之后添加了Get-AzureRmSubscription 并得到了这个异常:Get-AzureRmSubscription : Subscription XXX was not found in tenant . Please verify that the subscription exists in this tenant.
  • @MartinBrandl 针对您的场景的另一种解决方案,也许您可​​以使用Hybrid Runbook Worker。在本地 VM 中运行 Runbook。
  • 是的 Walter,该脚本在我的本地计算机上运行。虽然您的第二种方法可行,但我真的不想使用/尝试它。
  • 我发现报告了同样的问题here
  • 您好 Walter,我发现的最佳解决方法是使用证书凭据创建 RunAsAccount。我回答了我自己的问题,并在 Hybrid Runbook Worker 中添加了您的提示。非常感谢您的努力。
【解决方案3】:

以下是对我有用的代码(常规 dc 区域)。如果不起作用,请转到 自动化帐户 >> 模块 >> 更新 Azure 模块

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

更新

也许使用运行方式帐户运行可能是解决此问题的方法。通过导航到 Azure 自动化帐户 >> 帐户设置 >> 以帐户身份运行来创建一个。这是一个示例代码。

# 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

【讨论】:

  • 感谢您的努力。我没有将我的敏感数据存储在脚本代码中,这只是重现该问题的一个示例。此外,使用Login-AzureRmAccountAdd-AzureRmAccount 的登录确实可以正常工作(正如我所写的)。所以这并不能解决我的问题。
  • 您无需做任何其他事情。我还包括了 New-AzureRMSqlDatabaseExport cmdlet。全面测试。仅使用我提供的代码即可按预期工作。
  • 您阅读我的问题了吗?我有一个在本地运行没有任何问题的脚本。但是,当我在 Azure 自动化 Runbook 中运行它时,我收到一个错误。该问题可能与资源位于 MCD(Microsoft Cloud Deutschland)中并且 Runbook 托管在北欧云中这一事实有关。或者也许是别的东西。当你告诉我这不是我需要的其他东西时,它并不能解决我的问题。
  • 您的资源在哪里并不重要,是的,我阅读了您的问题,是的,我复制了您的环境进行测试。我通过自动化测试,而不是在我的本地机器上测试。祝你好运。
  • 对不起,布鲁诺,我有点粗鲁。谢谢你的努力。不幸的是,这在我的环境中不起作用。我还尝试设置一个新的自动化帐户但没有成功。如果我没有找到另一个解决方案,我会尝试设置一个新的服务主体,但奇怪的是相同的脚本在本地执行时可以工作。最好的问候
【解决方案4】:

看起来这是known issue,但我无法找到解决方法。但是有两个解决方法

  1. 使用混合 Runnbook Worker (mentioned by Walter - MSFT)
  2. 使用 RunAsAccountcertificate 凭据 (mentioned by Bruno Faria)

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

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

这是我用来从托管在 NorthEurope 的 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

【讨论】:

  • 很高兴它成功了。我仍在追赶微软。不确定它是错误还是“功能”(又名已知限制)。它已经存在了很长一段时间,所以他们应该意识到这一点。
猜你喜欢
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多