Azure 自动化帐户 Powershell 错误设置上下文

时间:2021-07-13 09:33:49

标签: azure azure-powershell azure-automation

我正在尝试使用 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 的选项。但是我不确定这里要给出什么。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在混合 PowerShell 模块。如果您使用的是 Az 模块,则需要使用 Connect-AzAccount 而不是 Add-AzureRmAccount。如果您使用的是 AzureRm 模块,则需要使用 Get-AzureRmSqlServerFirewallRule 而不是 Get-AzSqlServerFirewallRule