Azure Runbook - 在上下文中找不到订阅

标签 azure azure-powershell azure-runbook

背景:

我正在尝试通过 Azure 上的 Runbook 设置脚本。我发现不寻常的是,我可以通过 Azure Powershell Cloud Shell 运行该命令并且它可以工作。但是,当我尝试通过 Runbook 运行它时,我收到一个错误(见下文)。

脚本:

$ResourceGroupName = "group"
$ServerName = "serverName"
$DatabaseName = "databaseName"
$StorageKeyType = "StorageAccessKey"
$StorageKey = "storageKey"
$StorageUri = "storageUri"
$AdminLogin = "admin"
$AdminPassword = (ConvertTo-SecureString "12345" -AsPlainText -Force)

New-AzureRmSqlDatabaseExport `
        -AdministratorLogin $AdminLogin `
        -AdministratorLoginPassword $AdminPassword `
        -DatabaseName $DatabaseName `
        -ResourceGroupName $ResourceGroupName `
        -ServerName $ServerName `
        -StorageKey $StorageKey `
        -StorageKeyType $StorageKeyType `
        -StorageUri $StorageUri `

**使用通用值

错误:

New-AzureRmSqlDatabaseExport : No subscription found in the context.  Please ensure that the credentials you provided 
are authorized to access an Azure subscription, then run Connect-AzureRmAccount to login.
At line:10 char:1
+ New-AzureRmSqlDatabaseExport `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmSqlDatabaseExport], ApplicationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.NewAzureSqlDatabaseExport

问题:

我做错了什么?我使用的密码和用户名是在其他地方使用的密码和用户名,并且当我在 Cloud Shell 中运行脚本时可以使用。另外,“在上下文中找不到订阅”是什么意思?

最佳答案

在 Azure Cloud Shell 中,您已登录您的帐户,因此无需再次登录。但在 Runbook 中,您需要先登录您的帐户。

您可以使用以下代码登录。

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

在上面的代码中,您需要使用连接AzureRunAsConnection,它是Azure默认创建的,您可以直接使用它。

更多相关信息,您也可以查看这个question .

关于Azure Runbook - 在上下文中找不到订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49329676/

相关文章:

azure - 一个 Azure WebRole 中的多个站点

Azure 上的 Linux 虚拟机 - 如何应用 IP 限制

azure - Runbook 模块部署失败,因为它达到了终端预配状态 'Failed'

c# - 如何在 C# 中使用 Microsoft.Azure.Management.Automation AutomationClient 启动 Azure runbook/webhook?

azure - 我们可以使用 blob 绑定(bind)器将文件上传到 Azure 数据湖第 2 代吗

azure - 使用 ARM 策略限制用户访问资源组

powershell - 使用 PowerShell 配置 Ubuntu DSVM 时创建计划时出错

azure - 相当于 az acr login 的 powershell 是什么

azure - 使用 Powershell,如何向 azure api 管理 API 添加版本?

Azure Runbook - 作业无法停止