管理 Azure 自動化中的憑證

Azure 自動化使用 ,安全地儲存憑證以供 Runbook 和 DSC 組態存取適用于 Azure Resource Manager 資源的 Get-AzAutomationCertificate Cmdlet。 安全憑證存放區可供建立使用憑證進行驗證的 Runbook 和 DSC 設定,或將其新增至 Azure 或協力廠商資源。

注意

Azure 自動化中的安全資產包括認證、憑證、連接和加密的變數。 這些資產會使用針對每個自動化帳戶產生的唯一金鑰來加密並儲存在自動化中。 自動化會將金鑰儲存在系統管理的金鑰保存庫服務中。 在儲存安全資產之前,自動化會從金鑰保存庫載入金鑰,然後使用它來加密資產。

用來存取憑證的 PowerShell Cmdlet

下表中的 Cmdlet 會使用 PowerShell 建立和管理自動化憑證。 它們會隨附于 Az 模組中。

Cmdlet 描述
Get-AzAutomationCertificate 擷取 Runbook 或 DSC 組態中要使用的憑證相關資訊。 您只能使用內部 Get-AutomationCertificate Cmdlet 來擷取憑證本身。
New-AzAutomationCertificate 在自動化中建立新的憑證。
Remove-AzAutomationCertificate 從自動化移除憑證。
Set-AzAutomationCertificate 設定現有憑證的屬性,包括上傳憑證檔案,以及設定 .pfx 檔案的密碼

Add-AzureCertificate Cmdlet 也可用來上傳指定雲端服務的服務憑證。

存取憑證的內部 Cmdlet

下表中的內部 Cmdlet 可用來存取 Runbook 中的憑證。 此 Cmdlet 隨附于全域模組 Orchestrator.AssetManagement.Cmdlets 。 如需詳細資訊,請參閱 內部 Cmdlet

內部 Cmdlet 描述
Get-AutomationCertificate 取得要用於 Runbook 或 DSC 組態的憑證。 會傳 回 System.Security.Cryptography.X509Certificates.X509Certificate2 物件。

注意

您應該避免在 Name Runbook 或 DSC 組態的 參數 Get-AutomationCertificate 中使用變數。 這類變數在設計階段可能會使 Runbook 或 DSC 組態與自動化變數之間的相依性探索複雜化。

用來存取憑證的 Python 函式

使用下表中的 函式來存取 Python 2 和 3 Runbook 中的憑證。 Python 3 Runbook 目前為預覽狀態。

函式 描述
automationassets.get_automation_certificate 擷取憑證資產的相關資訊。

注意

您必須 automationassets 在 Python Runbook 開頭匯入模組,才能存取資產函式。

建立新的憑證

當建立新的憑證時,您會將 .cer 或 .pfx 檔案上傳到自動化。 如果您將憑證標示為可匯出,則可將其傳送到自動化憑證存放區外部。 如果不可匯出,則只能將其用於 Runbook 或 DSC 設定內的簽署。 自動化要求憑證具有提供者:Microsoft 增強的 RSA 與 AES 密碼編譯提供者

使用 Azure 入口網站建立新的憑證

  1. 從您的自動化帳戶,在左側窗格中,選取 [共用資源 ] 下的 [憑證 ]。
  2. 在 [ 憑證] 頁面上,選取 [新增憑證 ]。
  3. 在 [ 名稱] 欄位中,輸入憑證的名稱。
  4. 若要流覽 .cer .pfx 檔案,請在 [上傳憑證檔案 ] 底下 ,選擇 [ 選取檔案 ]。 如果您選取 .pfx 檔案,請指定密碼,並指出是否可以匯出。 如果您使用 Azure 自動化入口網站上傳憑證,合作夥伴 (CSP) 帳戶可能無法使用。 建議您使用 PowerShell Cmdlet 作為因應措施來克服此問題。
  5. 選取 [建立] 以儲存新的憑證資產。

使用 PowerShell 建立新的憑證

下列範例示範如何建立新的自動化憑證,並將其標示為可匯出。 此範例會匯入現有的 .pfx 檔案。

$certificateName = 'MyCertificate'
$PfxCertPath = '.\MyCert.pfx'
$CertificatePassword = ConvertTo-SecureString -String 'P@$$w0rd' -AsPlainText -Force
$ResourceGroup = "ResourceGroup01"

New-AzAutomationCertificate -AutomationAccountName "MyAutomationAccount" -Name $certificateName -Path $PfxCertPath -Password $CertificatePassword -Exportable -ResourceGroupName $ResourceGroup

使用 Resource Manager 範本建立新的憑證

下列範例示範如何透過 PowerShell 使用 Resource Manager 範本,將憑證部署至自動化帳戶:

$AutomationAccountName = "<automation account name>"
$PfxCertPath = '<PFX cert path and filename>'
$CertificatePassword = '<password>'
$certificateName = '<certificate name>' #A name of your choosing
$ResourceGroupName = '<resource group name>' #The one that holds your automation account
$flags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable `
    -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet `
    -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet
# Load the certificate into memory
$PfxCert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($PfxCertPath, $CertificatePassword, $flags)
# Export the certificate and convert into base 64 string
$Base64Value = [System.Convert]::ToBase64String($PfxCert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12))
$Thumbprint = $PfxCert.Thumbprint


$json = @"
{
    '`$schema': 'https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#',
    'contentVersion': '1.0.0.0',
    'resources': [
        {
            'name': '$AutomationAccountName/$certificateName',
            'type': 'Microsoft.Automation/automationAccounts/certificates',
            'apiVersion': '2015-10-31',
            'properties': {
                'base64Value': '$Base64Value',
                'thumbprint': '$Thumbprint',
                'isExportable': true
            }
        }
    ]
}
"@

$json | out-file .\template.json
New-AzResourceGroupDeployment -Name NewCert -ResourceGroupName $ResourceGroupName -TemplateFile .\template.json

取得憑證

若要擷取憑證,請使用內部 Get-AutomationCertificate Cmdlet。 您無法使用 Get-AzAutomationCertificate Cmdlet,因為它會傳回憑證資產的相關資訊,但不會傳回憑證本身的相關資訊。

文字 Runbook 範例

下列範例示範如何將憑證新增至 Runbook 中的雲端服務。 在此範例中,密碼是從加密的自動化變數擷取。

$serviceName = 'MyCloudService'
$cert = Get-AutomationCertificate -Name 'MyCertificate'
$certPwd = Get-AzAutomationVariable -ResourceGroupName "ResourceGroup01" `
-AutomationAccountName "MyAutomationAccount" -Name 'MyCertPassword'
Add-AzureCertificate -ServiceName $serviceName -CertToDeploy $cert

圖形化 Runbook 範例

以滑鼠右鍵按一下 [程式庫] 窗格中的憑證,然後選取 [新增至畫布 ],將內部 Get-AutomationCertificate Cmdlet 的活動新增至圖形化 Runbook。

Screenshot of adding a certificate to the canvas

下圖顯示圖形化 Runbook 中使用憑證的範例。

Screenshot of an example of graphical authoring

下一步