更新:

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

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

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

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:

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

最佳答案

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

Login-AzureRmAccount

然后从 Azure 获取订阅 ID 并使用 ID 而不是名称选择订阅,如下所示:
Select-AzureRmSubscription -SubscriptionId {insert-subscription-id}

关于powershell - 在 Azure 自动化 Runbook 中执行时出现 Set-AzureRmContext 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46428031/

10-11 21:41