背景:
我正在尝试通过 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/