azure - 2021 年,导出 Azure 自动化 AzureRunAsConnection 用于本地调试的证书的最快/最简单的方法是什么?

标签 azure powershell azure-automation service-principal runbook

作为对我们的 Runbook 之一所使用的服务主体的权限问题进行故障排除/诊断的一部分,Microsoft 支持人员要求我在本地运行相同的 PowerShell 代码,以便我们可以捕获日志。为此,我需要以与 Runbook 完全相同的方式进行身份验证,这意味着使用 AzureRunAsConnection 正在使用的证书进行身份验证。

将仅存在于 Azure 自动化中的一些 cmdlet 替换为 Azure RM 的等效命令后(例如,Get-AutomationConnection 必须替换为 Get-AzAutomationAccountGet-AzAutomationConnection,并且您必须切换到使用 FieldDefinitionValues),我的脚本的身份验证部分如下所示::

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$PSDefaultParameterValues['*:ErrorAction']='Stop'

'*** Authenticating with Azure'

$automationAccount = Get-AzAutomationAccount `
  -Name "MY_AUTOMATION_ACCOUNT" `
  -ResourceGroupName "MY_RESOURCE_GROUP"

$connection = $automationAccount | Get-AzAutomationConnection -Name "AzureRunAsConnection"

# Log-in to Az Account Management Graph API (for DNS updates)
Login-AzAccount -ServicePrincipal `
  -Tenant $connection.FieldDefinitionValues.TenantID `
  -ApplicationId $connection.FieldDefinitionValues.ApplicationID `
  -CertificateThumbprint $connection.FieldDefinitionValues.CertificateThumbprint

但是,当我运行它时,我收到此错误:

Login-AzAccount : No certificate was found in the certificate store with thumbprint
93FAB7F0BA11D08F8ABBAF5C587C77ECB058A8BB

看来我需要导出 AzureRunAsConnection 正在使用的证书,以便我可以将其导入到我的本地计算机中。但是,虽然我可以续订 Azure 自动化使用的证书或上传自己的证书,但似乎没有一种简单的方法来获取当前证书。我知道这是设计使然——为了安全——但对于这样的情况来说这是一个痛苦。

我发现 2018 年的这篇文章描述了如何在混合工作线程中导出证书,但该代码在 Azure Cloud Shell 和本地都不适用于我(Get-AutomationCertificate 未定义) ): https://www.lunavi.com/blog/how-to-download-an-azure-automation-connection-certificate-locally

网络上的一些指南建议创建一个 blob 存储帐户,然后编写一个脚本来导出到它,但这对于我只需要一次的复制来说似乎需要付出很大的努力。

获取此证书以进行本地调试/重现的最快、最简单的方法是什么?

最佳答案

最后,我使用 2018 年的文章设计了一种快速而肮脏的方法,从 Runbook 中获取可使用格式的证书。

我修改了 Runbook 中运行的脚本,在顶部添加了以下几行:

"*** Exporting Run As Certificate for Debugging"
$cert = Get-AutomationCertificate -Name "AzureRunAsCertificate"
$certData = $cert.Export("pfx", 'MySuperSecurePassword')

throw ([Convert]::ToBase64String($certData))

这会导致包含证书内容作为其消息的异常。然后,当我通过 Runbook“测试 Pane ”运行此代码时,我会在顶部以 Base64 格式转储证书的内容: The contents of the certificate in Base64-encoded format

我使用异常只是为了:1) Runbook 的其余部分不执行,2) 我保证看到该消息。我发现如果我在抛出异常之前执行 Write-Host,则在脚本停止运行之前输出可能不会被刷新。

然后,我可以复制括号中的 Base64 编码部分,将其粘贴到文件中,并使用 [System.Text.Encoding]::Unicode.GetString([ PowerShell 中的 System.Convert]::FromBase64String 或 WSL 中的 base64 --decode 来获取 PFX 文件。然后我可以打开该 PFX 文件并将其导入到我的本地用户存储中: Settings used for importing the PFX

关于azure - 2021 年,导出 Azure 自动化 AzureRunAsConnection 用于本地调试的证书的最快/最简单的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66283232/

相关文章:

azure - 仅针对 Azure 中特定 VM 上的警报采取措施

azure - 是否可以自动关闭 Azure VM 以节省成本?

azure - 来自 .Net Core 的 XMLA/TMSL

azure - 通用 Azure Functions - 动态连接

rest - 使用 REST API 创建 OS Azure 托管磁盘

Powershell wget 协议(protocol)违规

sql-server - PowerShell登录和特权

Azure 混合 worker Docker

azure - 使用 VirtualPathProvider 将主题放入 Azure CDN

windows - 如何在 Windows 10 企业版上运行应用程序作为 shell 替换