powershell - 如何在带有 AutomationConnection 的 Powershell Runbook 中使用 Connect-AzAccount
问题描述
我可以找到的所有对 Runbook 进行身份验证的示例都使用 AzureRM 模块:
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
但是,如果您使用的是新的 Az 模块,则此代码不起作用,并且您不能在同一个 Runbook 中混合使用 AzureRM 模块和Az 模块。如何获得身份验证以使用新的 Az 模块。
解决方案
此块可用于在 Runbook 中进行身份验证。请务必遵循模块配置说明: Azure 自动化中的 Az 模块支持
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$connectionResult = Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID `
-ApplicationId $servicePrincipalConnection.ApplicationID `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint `
-ServicePrincipal
"Logged in."
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
推荐阅读
- c# - 在使用 IoC 的单例类中不断更改属性的最佳设计?
- asp.net - 与 UserManage 不同的用户 ID
- excel - 在 VBA 中使用 SQL 时返回 Null 的值不是 Null
- javascript - 使用制表符 selectRow 方法以编程方式选择一行
- mongodb - 如何恢复已删除的文档 mongodb 3.6 单节点?
- if-statement - If Else 语句的时间复杂度
- html - 行高元素内的 inline-block 元素
- spring-boot - 使用外部化用户凭证连接 IBM MQ 的问题 - Spring Boot + JMS + IBM MQ 8.0.0.9
- java - Gradlew 守护程序无法保留足够的堆空间,但 RAM 足够空
- javascript - Scraping - 使用非恒定数据清理刮擦