关于 powershell:在 Azure 自动化 Runbook 中执行时出现 Set-AzureRmContext 错误 | 珊瑚贝

Set-AzureRmContext error when executed within an Azure Automation Runbook


更新:

好像其他人遇到了同样的问题并报告了它。

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

我已在 Azure Active Directory(托管在 Azure German Cloud 中)中添加了一个带有密码凭据的服务主体,并授予其参与者访问订阅(也托管在 Azure German Cloud 中)的权限。

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

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

1
2
3
4
5
6
SetAzureRmContext : Please provide a valid tenant or a valid subscription.
At line:26 char:1
+ SetAzureRmContext TenantId $TenantId Su …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [SetAzureRmContext], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

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

1
2
3
4
5
6
7
8
9
10
$TenantId =“”
$ApplicationId =“”
$ClientSecret =“”
$SubscriptionId =“”

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

AddAzureRmAccount ServicePrincipal Environment ‘AzureGermanCloud’ -Credential $mycreds TenantId $TenantId
SetAzureRmContext TenantId $TenantId SubscriptionId $SubscriptionId

我也尝试使用 Login-AzureRmAccount 没有成功。我还可以使用 Get-AzureRmResourceGroup cmdlet 来检索资源组,因此登录似乎可以正常工作。

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

TLTR:

我的主要目标是使用 runnbook 中的 New-AzureRmSqlDatabaseExport 启动 SQL 导出作业,但似乎上述错误会导致 cmdlet 失败并显示以下消息:

1
2
3
4
NewAzureRmSqlDatabaseExport : Your Azure credentials have not been set up or have expired, please run
LoginAzureRMAccount to set up your Azure credentials.
At line:77 char:18
+ … rtRequest = NewAzureRmSqlDatabaseExport 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?
  • 不,正如我提到的,它确实在本地运行。两个id都是正确的。
  • 默认情况下,每个自动化帐户都有 2 个 runas 帐户…我总是使用带有证书的那个…所以这不是您的选择吗?
  • @Kiran 我不想从托管在北欧的运行手册登录到托管在 Azure German Cloud 中的订阅。所以不幸的是没有。
  • @MartinBrandl 你试试 Add-AzureRmAccount -ServicePrincipal -Environment ‘AzureGermanCloud’ -Credential $mycreds -TenantId $TenantId -SubscriptionId $subscriptionId 吗?当您登录时使用指定的订阅 ID。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
$ClientSecret =“”
$ApplicationId =“”
$SubscriptionId =“”

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

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

#Export Database
NewAzureRmSqlDatabaseExport ResourceGroupName“<RG>” ServerName“<SQLSERVERNAME>” DatabaseName“<DATABASENAME>” StorageKeyType“StorageAccessKey” StorageKey“<STRKEY>” StorageUri“<URITOFILE>” AdministratorLogin“<DBLOGIN>” AdministratorLoginPassword“<DBPASS>”

更新

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

1
2
3
4
5
6
# Authenticate to Azure with service principal and certificate, and set subscription
$connectionAssetName =“AzureRunAsConnection”
$conn = GetAutomationConnection -Name $ConnectionAssetName

AddAzureRmAccount ServicePrincipal Tenant $conn.TenantID ApplicationId $conn.ApplicationId CertificateThumbprint $conn.CertificateThumbprint -ErrorAction Stop | Write-Verbose
SetAzureRmContext SubscriptionId $conn.SubscriptionId -ErrorAction Stop | Write-Verbose

  • 谢谢你的努力。我没有将我的敏感数据存储在脚本代码中,这只是重现该问题的一个示例。此外,使用 Login-AzureRmAccount 或 Add-AzureRmAccount 的登录确实可以正常工作(如我所写)。所以这并不能解决我的问题。
  • 你不需要做任何其他事情。我还包括了 New-AzureRMSqlDatabaseExport cmdlet。全面测试。仅使用我提供的代码即可按预期工作。
  • 你读过我的问题吗?我有一个在本地运行没有任何问题的脚本。但是,当我在 Azure 自动化 Runbook 中运行它时,我收到一个错误。该问题可能与资源位于 MCD(Microsoft Cloud Deutschland)中并且 Runbook 托管在北欧云中这一事实有关。或者也许是别的东西。当你告诉我这不是我需要的其他东西时,它并不能解决我的问题。
  • 您的资源在哪里并不重要,是的,我阅读了您的问题,是的,我复制了您的环境进行测试。我通过自动化测试,而不是在我的本地机器上测试。祝你好运。
  • 对不起布鲁诺,我有点粗鲁。谢谢你的努力。不幸的是,这在我的环境中不起作用。我还尝试设置一个新的自动化帐户但没有成功。如果我没有找到另一个解决方案,我会尝试设置一个新的服务主体,但奇怪的是相同的脚本在本地执行时可以工作。最好的祝福
  • 嗯。奇怪的。我不知道我们缺少什么。它应该是直截了当的。您确定此凭据有权访问订阅吗?我假设当您在本地运行时,您使用的是相同的主帐户,对吧?
  • 是的,这就是我目前将凭据存储在代码中的原因。如果我在本地执行脚本,一切正常。当我将其复制粘贴(不做任何更改)到运行手册(已尝试创建第二个帐户)时,我收到错误消息。
  • 我在 feedback.azure.com 上找到了一份报告(我编辑了我的问题)。不幸的是,没有答案。
  • 您认为您可以尝试使用运行方式帐户以防万一吗? docs.microsoft.com/en-us/azure/automation/…
  • 我可以试一试。不幸的是,该脚本不支持 AzureGermanCloud,所以我可能必须先采用它。我今天没有时间,但也许明天。无论如何,非常感谢您的努力!
  • 抱歉,最好通过门户进行。只需导航到自动化帐户>>向下滚动到帐户设置>>作为帐户运行>>创建。如果你已经有一个,就用它来代替。我用基于证书的身份验证示例更新了我的答案。
  • 我正在尝试使用 AzureGermanCloud 中的资源。但是那里还没有可用的 Azure 自动化服务。所以我无法通过门户创建 RunAsAccount。我可以尝试在 AzureGermanCloud 中使用 PowerShell 创建 RunAsAccount,并在 NorthEurope 的自动化帐户中添加资产。
  • 我没有使用诸如 German Cloud 之类的主权 Azure 数据中心的经验,但我正在与 Microsoft 的一些人核实这里有什么问题。希望我们对此有一个答案。
  • 谢谢布鲁诺。期待您的反馈。同时我尝试创建一个带有证书的runasaccount。
  • 我现在投入时间并在德国云中创建了一个带有客户端证书的 RunAsAccount,并手动将资产添加到自动化帐户中。您的解决方案几乎奏效了,您引导我走上了正确的道路,因此我给了您声誉。非常感谢。我自己回答了这个问题,因为我想描述这两种解决方法,而且我必须使用 Login-AzureRmAccount。


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

1
LoginAzureRmAccount

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

1
SelectAzureRmSubscription SubscriptionId {insertsubscriptionid}
  • 谢谢克莱夫,我会试一试,然后回来找你
  • 不客气,以防万一您也尝试这样做:”Add-AzureAccount”,它登录到您的 Azure 帐户并添加您的帐户。
  • 感谢您的提示,但我怀疑它是否会有所帮助,因为我使用的是 Azure 资源管理器 (RM) cmdlet。
  • 不幸的是,我收到错误消息:Select-AzureRmSubscription : Please provide a valid tenant or a valid subscription
  • 您可以将服务主体角色从贡献者更改为所有者,以确保它不是权限问题吗?尽管贡献者也应该能够管理一切。或者是否可以再次运行”Login-AzureRmAccount”并在本地使用为服务主体创建的帐户?
  • 我尝试将权限设置为所有者,但收到相同的错误(脚本也在本地工作)。 New-AzureRmSqlDatabaseExport 不采用任何上下文参数,因此您的第二个想法对我没有帮助。但非常感谢您的努力。
  • @CliveCiappara。谢谢 只是不明白为什么在微软文档中他们可以给出这样的一行示例。文档过于冗长,仍然没有涵盖基本的用例。


这似乎是一个已知问题,我无法找到解决方法。但是有两种解决方法:

  • 使用混合 Runnbook Worker(Walter 提到 – MSFT)
  • 使用带有证书凭据的 RunAsAccount(Bruno Faria 提到)

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

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

    这是我用来从托管在 NorthEurope 的 Azure Runbook 登录到 AzureGermanCloud (MCD) 的代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $connectionAssetName =“AzureRunAsConnection”
    $conn = GetAutomationConnection -Name $ConnectionAssetName

    LoginAzureRmAccount `
        ServicePrincipal `
        CertificateThumbprint $conn.CertificateThumbprint `
        ApplicationId $conn.ApplicationId `
        TenantId $conn.TenantID `
        Environment AzureGermanCloud

    • 很高兴它奏效了。我仍在追赶微软。不确定它是错误还是”功能”(又名已知限制)。它已经存在了很长一段时间,所以他们应该意识到这一点。


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

    1
    2
    3
    4
    5
    6
    7
    $subscriptionId=“”
    $tenantid=“”
    $clientid=“”
    $password=“”
    $userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
    $userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientid, $userPassword
    AddAzureRmAccount 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,我发现的最佳解决方法是使用证书凭据创建 RunAsAccount。我回答了我自己的问题,并在 Hybrid Runbook Worker 中添加了您的提示。非常感谢您的努力。


    来源:https://www.codenong.com/46428031/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9095.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?