0

背景:

我正在尝试通过 Azure 上的 Runbooks 设置脚本。我发现不寻常的是,我可以通过 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 中运行脚本时工作。另外,“在上下文中找不到订阅”是什么意思?

4

2 回答 2

1

这意味着您需要先登录 Azure,然后才能执行任何操作。Cloud Shell 会为您处理这些,而 Azure 自动化则不会。

https://docs.microsoft.com/en-us/powershell/azure/get-started-azureps?view=azurermps-5.5.0#log-in-to-azure

您可以使用 Azure AD 用户登录、证书登录或服务主体登录。真实账户不会工作,因为它是交互式的。

于 2018-03-16T21:21:03.177 回答
1

在 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 默认创建的,你可以直接使用它。

有关此的更多信息,您也可以查看此问题

于 2018-03-19T07:38:08.160 回答