首页 > 解决方案 > Azure 自动化帐户 Powershell 错误设置上下文

问题描述

我正在尝试使用 Azure 自动化帐户运行一个简单的 powershell runbook。我有一个 RunasAccount 设置,它对订阅有贡献者特权,我试图在我的一个 Sql 服务器中获取列入白名单的 IP 列表。

Import-Module Az.Sql
Import-Module Az.Accounts
$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
    }
}


Get-AzSqlServerFirewallRule -ResourceGroupName test-rg -ServerName test-server101 

当我运行它时,我收到以下错误。

Get-AzSqlServerFirewallRule :在上下文中找不到订阅。请确保您提供的凭据有权访问 Azure 订阅,然后运行 ​​Connect-AzAccount 登录。在 line:36 char:1 + Get-AzSqlServerFirewallRule -ResourceGroupName test-rg -ServerName te ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzSqlServerFirewallRule],AzPSApplicationException + FullyQualifiedErrorId:Microsoft.Azure.Commands.Sql.FirewallRule.Cmdlet.GetAzureSqlServerFirewallRule

我注意到 Get-AzSqlServerFirewallRule 命令行开关有一个设置 -DefaultProfile 的选项。但是我不确定这里要给出什么。

我在这里做错了什么?

标签: azureazure-powershellazure-automation

解决方案


You're mixing PowerShell modules. If you're using the Az module, then you need to use Connect-AzAccount rather than Add-AzureRmAccount. If you're using the AzureRm module, then you need to use Get-AzureRmSqlServerFirewallRule rather than Get-AzSqlServerFirewallRule.


推荐阅读