共用方式為


針對自動化案例,以非互動方式登入 Azure PowerShell

Azure 中的受控識別提供安全且順暢的方式,讓應用程式、服務和自動化工具存取 Azure 資源,而不需要將認證儲存在程式碼或組態中。 不同於需要手動認證管理的服務主體,Azure 會自動處理受控識別,而且不會公開敏感性秘密。 使用受控識別是撰寫安全自動化腳本的最佳做法,因為它可簡化驗證,並將認證外泄的風險降到最低。 受控識別也可協助安全地自動化管理工作,而不需要依賴使用者身分識別。 受控識別的許可權是透過 Microsoft Entra 來管理,確保只有必要的資源存取權,增強安全性和可維護性。

重要

從 2025 年初開始,使用 Microsoft Entra ID 使用者身分驗證至 Azure 時將需要多重驗證 (MFA)。 如需詳細資訊,請參閱 自動化案例中的多重要素驗證對 Azure PowerShell 的影響

先決條件

使用受控識別登入

受控識別是一種特殊的服務主體類型,可為 Azure 服務提供自動受控識別。 使用此類型的身分識別不需要將認證儲存在組態或程序代碼中,即可向任何支援受控識別的 Azure 服務進行驗證。

受控識別有兩種類型:

  • 系統指派的受控識別
  • 使用者指派的管理識別

受控識別提供與其他 Azure 服務通訊的安全方式,而開發人員不需要管理認證。 它們也有助於降低認證外泄的風險。

以下是受控識別在真實世界中的運作方式:

  • Azure 會自動管理建立和刪除受控識別所使用的認證。
  • 使用受控識別啟用的 Azure 服務,可能會使用 Microsoft Entra 令牌安全地存取其他服務,例如 Azure Key Vault、Azure SQL Database、Azure Blob 記憶體等。
  • 此身分識別會直接在 Azure 內管理,而不需要額外的布建。

受控識別可藉由避免儲存和管理認證的需求來簡化安全性模型,並藉由降低與處理秘密相關聯的風險,在安全雲端作業中扮演重要角色。

系統指派的受控識別

Azure 會自動為 Azure 服務實例建立系統指派的受控識別(例如 Azure VM、App Service 或 Azure Functions)。 刪除服務實例時,Azure 會自動清除與服務相關聯的認證和身分識別。

下列範例會使用主機環境的系統指派受控識別進行連線。 如果在具有指派受控識別的虛擬機上執行,它可讓程式代碼使用指派的身分識別登入。

 Connect-AzAccount -Identity

使用者指派的受管理的身分識別

使用者指派的受控身分識別是您在 Microsoft Entra 中建立和管理的識別。 它可以指派給一或多個 Azure 服務實例。 使用者指派受控識別的生命週期會與其指派的服務實例分開管理。

使用使用者指派的受控識別時,您必須指定 AccountIdIdentity 參數,如下列範例所示。

 Connect-AzAccount -Identity -AccountId <user-assigned-identity-clientId-or-resourceId>

下列命令會使用 myUserAssignedIdentity的受控識別進行連線。 它會將使用者指派的身分識別新增至虛擬機,然後使用使用者指派身分識別 ClientId 進行連線。

$identity = Get-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Name myUserAssignedIdentity
Get-AzVM -ResourceGroupName contoso -Name testvm | Update-AzVM -IdentityType UserAssigned -IdentityId $identity.Id
Connect-AzAccount -Identity -AccountId $identity.ClientId # Run on the virtual machine
Account                              SubscriptionName TenantId                             Environment
-------                              ---------------- --------                             -----------
00000000-0000-0000-0000-000000000000 My Subscription  00000000-0000-0000-0000-000000000000 AzureCloud

如需詳細資訊,請參閱 在 Azure VM 上設定 Azure 資源的受控識別

使用服務主體登入

若要使用服務主體登入,請使用 Connect-AzAccount Cmdlet 的 ServicePrincipal 參數。 您也需要服務主體的下列資訊:

  • AppId
  • 登入認證或用來建立服務主體的憑證的存取權限
  • 租戶識別碼

使用服務主體登入的方式取決於其設定為憑證型或密碼型驗證。

憑證式驗證

若要瞭解如何建立 Azure PowerShell 的服務主體,請參閱 使用 Azure PowerShell 建立 Azure 服務主體

憑證式驗證需要 Azure PowerShell 根據憑證指紋,從本機證書存儲擷取資訊。

Connect-AzAccount -ApplicationId $appId -Tenant $tenantId -CertificateThumbprint <thumbprint>

使用服務主體而非已註冊的應用程式時,請指定 ServicePrincipal 參數,並提供服務主體的 AppId 作為 ApplicationId 參數的值。

Connect-AzAccount -ServicePrincipal -ApplicationId $servicePrincipalId -Tenant $tenantId -CertificateThumbprint <thumbprint>

在 Windows PowerShell 5.1 中,您可以使用 PKI 模組來管理和檢查證書存儲。 對於 PowerShell 7.x 及其後版本,處理方式不同。 下列腳本示範如何將現有的憑證匯入 PowerShell 可存取的證書存儲。

在 PowerShell 7.x 和更新版本中匯入憑證

# Import a PFX
$storeName = [System.Security.Cryptography.X509Certificates.StoreName]::My
$storeLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $storeLocation)
$certPath = <path to certificate>
$credentials = Get-Credential -Message "Provide PFX private key password"
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($certPath, $credentials.Password, $flag)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($Certificate)
$store.Close()

在 Windows PowerShell 5.1 中匯入憑證

# Import a PFX
$credentials = Get-Credential -Message 'Provide PFX private key password'
Import-PfxCertificate -FilePath <path to certificate> -Password $credentials.Password -CertStoreLocation cert:\CurrentUser\My

密碼型驗證

建立要與本節範例搭配使用的服務主體。 如需建立服務主體的詳細資訊,請參閱 使用 Azure PowerShell 建立 Azure 服務主體

$sp = New-AzADServicePrincipal -DisplayName ServicePrincipalName

謹慎

所提供的服務主體秘密會儲存在使用者配置檔的 AzureRmContext.json 檔案中($env:USERPROFILE\.Azure)。 請確定此目錄具有適當的保護。

若要取得服務主體的認證做為物件,請使用 Get-Credential Cmdlet。 此 Cmdlet 會提示輸入使用者名稱和密碼。 使用服務主體的用戶名稱 AppId,並將其 secret 轉換成密碼的純文本。

# Retrieve the plain text password for use with Get-Credential in the next command.
$sp.PasswordCredentials.SecretText

$pscredential = Get-Credential -UserName $sp.AppId
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

針對自動化情境,您必須從服務主體的 AppIdSecretText建立憑證:

$SecureStringPwd = $sp.PasswordCredentials.SecretText | ConvertTo-SecureString -AsPlainText -Force
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sp.AppId, $SecureStringPwd
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

在自動化服務主體連線時,請使用適當的密碼記憶體做法。

另請參閱