azure - 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 的选项。但是我不确定这里要给出什么。
我在这里做错了什么?
解决方案
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
.
推荐阅读
- c# - NServiceBus.LicenseManager 找不到有效的许可证
- javascript - 解析作为 func 参数接收的对象
- c# - 没有另一个列表元素的 C# 列表(集合论,补码)
- c# - Azure DevOps 中的错误 CS0136 但不是本地错误
- c# - 使用 JsonConvert 从 .Net 中的 JSON 中删除/替换换行符
- javascript - 如何使用jQuery获取没有星期天的所有日期?
- c# - 如何使异步 UdpClient 接收超时并获得响应?
- android - 在 Android 中使用 Lru Cache 缓存数据
- sql - SQL | 显示日期不是当前月份的所有行
- python-3.x - 解决线程中的组参数错误