這很重要
Azure 磁碟加密預定於 2028 年 9 月 15 日退休。 在此之前,您可以繼續使用 Azure 磁碟加密,不會中斷。 2028 年 9 月 15 日,啟用 ADE 的工作負載仍可繼續運行,但加密磁碟在虛擬機重啟後將無法解鎖,導致服務中斷。
對於新虛擬機,可以在主機上使用 加密 ,或考慮 機密虛擬機大小搭配作業系統磁碟加密 來處理機密運算工作負載。 所有啟用 ADE 的虛擬機(包括備份)必須在退休日前遷移至主機加密,以避免服務中斷。 詳情請參見「 從 Azure 磁碟加密遷移到主機加密 」。
適用於:✔️ Windows VM
新版的 Azure 磁碟加密不需要提供 Microsoft Entra 應用程式參數,即可啟用 VM 磁碟加密。 在新版本中,啟用加密步驟時,你不再需要提供 Microsoft Entra 憑證。 使用新版本加密所有虛擬機,且不使用Microsoft Entra應用程式參數。 關於使用新版本啟用虛擬機磁碟加密的說明,請參見 Azure 磁碟加密。 已經用 Microsoft Entra 應用程式參數加密的虛擬機仍然被支援。 繼續用 Microsoft Entra 語法維護那些虛擬機。
Azure 磁碟加密會使用 Azure Key Vault 來控制及管理磁碟加密金鑰和祕密。 如需金鑰保存庫的詳細資訊,請參閱開始使用 Azure Key Vault 和保護金鑰保存庫。
建立並設定用於 Azure 磁碟加密 與 Microsoft Entra ID(前版本)使用的金鑰保險庫,包含四個步驟:
- 建立金鑰保存庫。
- 設定 Microsoft Entra 應用程式和服務主體。
- 設定 Microsoft Entra 應用程式的金鑰保存庫存取原則。
- 設定金鑰保存庫進階存取原則。
您也可以產生或匯入金鑰加密金鑰 (KEK)。
請參閱主文「為 Azure 磁碟加密 建立並配置金鑰庫」文章,了解如何安裝工具並連接 Azure。
注意
本文中的步驟會在 Azure 磁碟加密的必要 CLI 指令碼中和 Azure 磁碟加密的必要 PowerShell 指令碼中自動化。
建立金鑰保存庫
Azure 磁碟加密會與 Azure Key Vault 整合,協助您控制及管理金鑰保存庫訂用帳戶中的磁碟加密金鑰與祕密。 針對 Azure 磁碟加密,您可以建立金鑰保存庫,也可以使用現有保存庫。 如需金鑰保存庫的詳細資訊,請參閱開始使用 Azure Key Vault 和保護金鑰保存庫。 您可以使用 Resource Manager 範本、Azure PowerShell 或 Azure CLI 來建立金鑰保存庫。
警告
為了確保加密秘密不會跨越區域邊界,Azure 磁碟加密 要求金鑰庫和虛擬機必須在同一區域。 在你想加密的虛擬機所在區域建立並使用金鑰庫。
使用 PowerShell 建立金鑰保險庫
你可以使用 Azure PowerShell 和 New-AzKeyVault cmdlet 來建立金鑰庫。 關於其他 金鑰保存庫 指令,請參見 Az.KeyVault。
如有需要,請使用 New-AzResourceGroup 建立新的資源群組。 若要列出資料中心位置,請使用 Get-AzLocation。
# Get-AzLocation New-AzResourceGroup –Name 'MyKeyVaultResourceGroup' –Location 'East US'使用 New-AzKeyVault 建立新的金鑰保險庫
New-AzKeyVault -VaultName 'MySecureVault' -ResourceGroupName 'MyKeyVaultResourceGroup' -Location 'East US'請記下系統回傳的 保存庫名稱、資源群組名稱、資源識別碼、保存庫 URI,以及 物件 ID,以供稍後加密磁碟時使用。
使用 Azure CLI 建立金鑰保存庫
你可以用 Azure CLI 和 az keyvault 指令來管理你的金鑰庫。 若要建立金鑰保存庫,請使用 az keyvault create。
如有需要,請使用 az group create 建立新的資源群組。 若要列出位置,請使用 az account list-locations
# To list locations: az account list-locations --output table az group create -n "MyKeyVaultResourceGroup" -l "East US"使用 az keyvault create 建立新的金鑰庫。
az keyvault create --name "MySecureVault" --resource-group "MyKeyVaultResourceGroup" --location "East US"請注意 Vault 名稱 (名稱)、 資源群組名稱、 資源 ID (ID)、 Vault URI 以及物件 ID ,這些都是日後回傳可使用的。
使用 Resource Manager 範本建立金鑰保險庫
您可以使用 Resource Manager 範本來建立金鑰保存庫。
- 在 Azure 快速入門範本上,選取 [部署至 Azure]。
- 選擇訂閱、資源群組、資源群組位置、金鑰庫名稱、物件 ID、法律條款和協議,然後選擇 購買。
設定 Microsoft Entra 應用程式和服務主體
當您需要在 Azure 中執行中的 VM 上啟用加密時,Azure 磁碟加密會產生並將加密金鑰寫入金鑰保存庫。 在金鑰保存庫中管理加密金鑰需要 Microsoft Entra 驗證。 為此目的建立 Microsoft Entra 應用程式。 就驗證目的而言,您可以使用用戶端密碼式驗證,或用戶端憑證式 Microsoft Entra 驗證。
使用 Azure PowerShell 設定 Microsoft Entra 應用程式和服務主體
要執行以下指令,請取得並使用 Azure PowerShell 模組。
使用 New-AzADApplication PowerShell Cmdlet 建立 Microsoft Entra 應用程式。 MyApplicationHomePage 和 MyApplicationUri 可以是你選擇的任何值。
$aadClientSecret = "My AAD client secret" $aadClientSecretSec = ConvertTo-SecureString -String $aadClientSecret -AsPlainText -Force $azureAdApplication = New-AzADApplication -DisplayName "My Application Display Name" -HomePage "https://MyApplicationHomePage" -IdentifierUris "https://MyApplicationUri" -Password $aadClientSecretSec $servicePrincipal = New-AzADServicePrincipal –ApplicationId $azureAdApplication.ApplicationId -Role Contributor$azureAdApplication.ApplicationId是 Microsoft Entra 的用戶端 ID,而 $aadClientSecret 是您稍後將用來啟用 Azure 磁碟加密 的用戶端密碼。 妥善保護 Microsoft Entra 用戶端密碼。 執行時$azureAdApplication.ApplicationId會顯示應用程式 ID。
使用 Azure CLI 設定 Microsoft Entra 應用程式和服務主體
你可以用 Azure CLI 和 az ad sp 指令來管理你的服務主體。 如需詳細資訊,請參閱建立 Azure 服務主體。
建立新的服務主體。
az ad sp create-for-rbac --name "ServicePrincipalName" --password "My-AAD-client-secret" --role Contributor --scopes /subscriptions/<subscription_id>回傳的 appID 是其他指令中使用的 Microsoft Entra 用戶端 ID。 這也是您在 az keyvault set-policy 中要使用的 SPN。 密碼是你之後用來啟用 Azure 磁碟加密 的用戶端秘密。 妥善保護 Microsoft Entra 用戶端密碼。
透過 Azure 入口網站設定 Microsoft Entra 應用程式和服務主體
使用使用入口網站來建立可存取資源的 Microsoft Entra 應用程式和服務主體這篇文章中的步驟,來建立 Microsoft Entra 應用程式。 這些步驟都會直接帶你到文章部分完成。
- 確認所需權限
-
建立 Microsoft Entra 應用程式
- 在建立應用程式時,您可以使用任何您想要的名稱和登入 URL。
-
取得應用程式識別碼和驗證金鑰。
- 驗證金鑰就是用戶端密碼,可作為 Set-AzVMDiskEncryptionExtension 的 AadClientSecret。
- 應用程式會使用驗證金鑰作為認證,藉此登入 Microsoft Entra ID。 在 Azure 入口網站中,此密碼稱為金鑰,但實際上與金鑰保存庫並無任何關聯。 請適當地保護這個祕密。
- 應用程式 ID 稍後將用作 Set-AzVMDiskEncryptionExtension 的 AadClientId,以及 Set-AzKeyVaultAccessPolicy 的 ServicePrincipalName。
設定 Microsoft Entra 應用程式的金鑰保存庫存取原則
為了將加密祕密寫入指定的金鑰保存庫,Azure 磁碟加密需要 Microsoft Entra 應用程式的用戶端識別碼和用戶端密碼,該識別碼和密碼需有權將祕密寫入金鑰保存庫。
注意
若要使用 Azure 磁碟加密,您必須對 Microsoft Entra 用戶端應用程式設定下列存取原則:WrapKey 和 Set 權限。
使用 Azure PowerShell 設定 Microsoft Entra 應用程式的金鑰保存庫存取原則
您的 Microsoft Entra 應用程式需要權限,才能存取保存庫中的金鑰或祕密。 使用 Set-AzKeyVaultAccessPolicy Cmdlet 可授與應用程式權限,使用用戶端識別碼 (登錄應用程式時所產生) 作為 -ServicePrincipalName 參數值。 若要深入了解,請參閱部落格文章 Azure Key Vault - 逐步解說。
使用 PowerShell 設定 AD 應用程式的金鑰保險庫存取政策。
$keyVaultName = 'MySecureVault' $aadclient ID = 'MyAadAppclient ID' $KVRGname = 'MyKeyVaultResourceGroup' Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -ServicePrincipalName $aadclient ID -PermissionsToKeys 'WrapKey' -PermissionsToSecrets 'Set' -ResourceGroupName $KVRGname
使用 Azure CLI 為 Microsoft Entra 應用程式設定 Key Vault 存取原則
使用 az keyvault set-policy 來設定存取原則。 如需詳細資訊,請參閱使用 CLI 2.0 管理 金鑰保存庫。
請用以下指令授權你用 Azure CLI 建立的服務主體取得秘密和封裝金鑰:
az keyvault set-policy --name "MySecureVault" --spn "<spn created with CLI/the Microsoft Entra client ID>" --key-permissions wrapKey --secret-permissions set
透過入口網站設定 Microsoft Entra 應用程式的金鑰保險庫存取政策
- 使用金鑰保存庫開啟資源群組。
- 選擇你的金鑰保險庫,前往存取政策,然後選擇新增。
- 在 [選取主體] 底下,搜尋您所建立的 Microsoft Entra 應用程式,並加以選取。
- 關於金鑰權限,請在密碼操作下勾選 Wrap Key。
- 關於秘密權限,請在秘密管理操作中勾選設定。
- 選取 [確定] 以儲存存取原則。
設定金鑰保存庫進階存取原則
Azure 平台需要存取您金鑰保存庫中的加密金鑰或密碼,讓該資訊可供 VM 用來開機和解密磁碟區。 啟用金鑰庫的磁碟加密,否則部署失敗。
使用 Azure PowerShell 設定金鑰保存庫的進階存取原則
使用金鑰保存庫 PowerShell Cmdlet Set-AzKeyVaultAccessPolicy 以啟用金鑰保存庫的磁碟加密。
針對磁碟加密啟用 金鑰保存庫:Azure 磁碟加密需要 EnabledForDiskEncryption。
Set-AzKeyVaultAccessPolicy -VaultName 'MySecureVault' -ResourceGroupName 'MyKeyVaultResourceGroup' -EnabledForDiskEncryption視需要針對部署啟用 金鑰保存庫:可讓 Microsoft.Compute 資源提供者在資源建立期間 (例如,在建立虛擬機器時) 參考了這個金鑰保存庫的情況下,從這個金鑰保存庫擷取祕密。
Set-AzKeyVaultAccessPolicy -VaultName 'MySecureVault' -ResourceGroupName 'MyKeyVaultResourceGroup' -EnabledForDeployment視需要針對範本部署啟用 金鑰保存庫:可讓 Azure Resource Manager 在範本部署參考了這個金鑰保存庫的情況下,從這個金鑰保存庫取得祕密。
Set-AzKeyVaultAccessPolicy -VaultName 'MySecureVault' -ResourceGroupName 'MyKeyVaultResourceGroup' -EnabledForTemplateDeployment
使用 Azure CLI 設定金鑰保存庫進階存取原則
使用 az keyvault update 啟用金鑰保存庫的磁碟加密。
針對磁碟加密啟用金鑰保存庫:需要啟用磁碟加密功能。
az keyvault update --name "MySecureVault" --resource-group "MyKeyVaultResourceGroup" --enabled-for-disk-encryption "true"視需要針對部署啟用 金鑰保存庫:允許虛擬機器從保存庫擷取儲存為祕密的憑證。
az keyvault update --name "MySecureVault" --resource-group "MyKeyVaultResourceGroup" --enabled-for-deployment "true"視需要針對範本部署啟用 金鑰保存庫:允許 Resource Manager 從保存庫擷取祕密。
az keyvault update --name "MySecureVault" --resource-group "MyKeyVaultResourceGroup" --enabled-for-template-deployment "true"
透過 Azure 入口網站設定金鑰保存庫進階存取原則
- 選擇你的金鑰保險庫,前往 存取政策, 然後點擊顯示進階存取政策。
- 選取啟用對 Azure 磁碟加密的存取進行磁碟區加密的標示方塊。
- 如果需要,選擇啟用 Access to Azure 虛擬機器 以進行部署,或選擇啟用 Access to Azure Resource Manager 以進行範本部署。
- 選取 [儲存]。
設定主要加密金鑰 (可選)
如果您想使用金鑰加密金鑰(KEK)來增加加密金鑰的安全層,請在您的金鑰庫中新增 KEK。 使用 Add-AzKeyVaultKey Cmdlet 在金鑰保存庫中建立金鑰加密金鑰。 您也可以從內部部署金鑰管理 HSM 匯入 KEK。 欲了解更多資訊,請參閱 金鑰保存庫 文件。 若指定了金鑰加密金鑰,Azure 磁碟加密會先使用該金鑰包裝加密祕密,再寫入 金鑰保存庫。
產生金鑰時,請使用 RSA 金鑰類型。 Azure 磁碟加密 目前還不支援使用橢圓曲線金鑰。
您的金鑰保存庫機密和 KEK(金鑰加密金鑰)URL 必須有版本。 Azure 會強制執行設定版本的這項限制。 針對有效的密碼和 KEK URL,請參閱下列範例︰
有效祕密 URL 的範例:
https://contosovault.vault.azure.net/secrets/EncryptionSecretWithKek/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx有效 KEK URL 的範例:
https://contosovault.vault.azure.net/keys/diskencryptionkek/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAzure 磁碟加密不支援將連接埠號碼指定為金鑰保存庫密碼和 KEK URL 的一部分。 如需不支援和支援的金鑰保存庫 URL 範例,請參閱下列範例:
無法接受的金鑰保存庫 URL
https://contosovault.vault.azure.net:443/secrets/contososecret/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx可接受的金鑰保存庫 URL:
https://contosovault.vault.azure.net/secrets/contososecret/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
使用 Azure PowerShell 設定金鑰加密金鑰
在使用 PowerShell 腳本之前,先先用 Azure 磁碟加密 的前置條件來了解腳本中的步驟。 範例指令碼可能需要隨環境加以變更。 此指令碼會建立所有 Azure 磁碟加密必要條件,並加密現有的 IaaS VM,方法是使用金鑰加密金鑰包裝磁碟加密金鑰。
# Step 1: Create a new resource group and key vault in the same location.
# Fill in 'MyLocation', 'MyKeyVaultResourceGroup', and 'MySecureVault' with your values.
# Use Get-AzLocation to get available locations and use the DisplayName.
# To use an existing resource group, comment out the line for New-AzResourceGroup
$Loc = 'MyLocation';
$KVRGname = 'MyKeyVaultResourceGroup';
$KeyVaultName = 'MySecureVault';
New-AzResourceGroup –Name $KVRGname –Location $Loc;
New-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname -Location $Loc;
$KeyVault = Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname;
$KeyVaultResourceId = (Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname).ResourceId;
$diskEncryptionKeyVaultUrl = (Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname).VaultUri;
# Step 2: Create the AD application and service principal.
# Fill in 'MyAADClientSecret', "<My Application Display Name>", "<https://MyApplicationHomePage>", and "<https://MyApplicationUri>" with your values.
# MyApplicationHomePage and the MyApplicationUri can be any values that you choose.
$aadClientSecret = 'MyAADClientSecret';
$aadClientSecretSec = ConvertTo-SecureString -String $aadClientSecret -AsPlainText -Force;
$azureAdApplication = New-AzADApplication -DisplayName "<My Application Display Name>" -HomePage "<https://MyApplicationHomePage>" -IdentifierUris "<https://MyApplicationUri>" -Password $aadClientSecretSec
$servicePrincipal = New-AzADServicePrincipal –ApplicationId $azureAdApplication.ApplicationId -Role Contributor;
$aadclient ID = $azureAdApplication.ApplicationId;
#Step 3: Enable the vault for disk encryption and set the access policy for the Microsoft Entra application.
Set-AzKeyVaultAccessPolicy -VaultName $KeyVaultName -ResourceGroupName $KVRGname -EnabledForDiskEncryption;
Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -ServicePrincipalName $aadclient ID -PermissionsToKeys 'WrapKey' -PermissionsToSecrets 'Set' -ResourceGroupName $KVRGname;
#Step 4: Create a new key in the key vault with the Add-AzKeyVaultKey cmdlet.
# Fill in 'MyKeyEncryptionKey' with your value.
$keyEncryptionKeyName = 'MyKeyEncryptionKey';
Add-AzKeyVaultKey -VaultName $KeyVaultName -Name $keyEncryptionKeyName -Destination 'Software';
$keyEncryptionKeyUrl = (Get-AzKeyVaultKey -VaultName $KeyVaultName -Name $keyEncryptionKeyName).Key.kid;
#Step 5: Encrypt the disks of an existing IaaS VM
# Fill in 'MySecureVM' and 'MyVirtualMachineResourceGroup' with your values.
$VMName = 'MySecureVM';
$VMRGName = 'MyVirtualMachineResourceGroup';
Set-AzVMDiskEncryptionExtension -ResourceGroupName $VMRGName -VMName $vmName -Aadclient ID $aadclient ID -AadClientSecret $aadClientSecret -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId -KeyEncryptionKeyUrl $keyEncryptionKeyUrl -KeyEncryptionKeyVaultId $KeyVaultResourceId;
憑證式驗證 (選擇性)
如果你想使用憑證認證,可以將憑證上傳到你的金鑰庫,然後部署到用戶端。 在使用 PowerShell 腳本之前,先先用 Azure 磁碟加密 的前置條件來了解腳本中的步驟。 範例指令碼可能需要隨環境加以變更。
# Fill in "MyKeyVaultResourceGroup", "MySecureVault", and 'MyLocation' ('My location' only if needed)
$KVRGname = 'MyKeyVaultResourceGroup'
$KeyVaultName= 'MySecureVault'
# Create a key vault and set enabledForDiskEncryption property on it.
# Comment out the next three lines if you already have an existing key vault enabled for encryption. No need to set 'My location' in this case.
$Loc = 'MyLocation'
New-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname -Location $Loc
Set-AzKeyVaultAccessPolicy -VaultName $KeyVaultName -ResourceGroupName $KVRGname -EnabledForDiskEncryption
#Setting some variables with the key vault information
$KeyVault = Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname
$DiskEncryptionKeyVaultUrl = $KeyVault.VaultUri
$KeyVaultResourceId = $KeyVault.ResourceId
# Create the Microsoft Entra application and associate the certificate with it.
# Fill in "C:\certificates\mycert.pfx", "Password", "<My Application Display Name>", "<https://MyApplicationHomePage>", and "<https://MyApplicationUri>" with your values.
# MyApplicationHomePage and the MyApplicationUri can be any values you wish
$CertPath = "C:\certificates\mycert.pfx"
$CertPassword = "Password"
$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath, $CertPassword)
$CertValue = [System.Convert]::ToBase64String($cert.GetRawCertData())
$AzureAdApplication = New-AzADApplication -DisplayName "<My Application Display Name>" -HomePage "<https://MyApplicationHomePage>" -IdentifierUris "<https://MyApplicationUri>" -CertValue $CertValue
$ServicePrincipal = New-AzADServicePrincipal -ApplicationId $AzureAdApplication.ApplicationId -Role Contributor
$AADclient ID = $AzureAdApplication.ApplicationId
$aadClientCertThumbprint= $cert.Thumbprint
Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -ServicePrincipalName $aadclient ID -PermissionsToKeys 'WrapKey' -PermissionsToSecrets 'Set' -ResourceGroupName $KVRGname
# Upload the pfx file to the key vault.
# Fill in "MyAADCert".
$KeyVaultSecretName = "MyAADCert"
$FileContentBytes = get-content $CertPath -Encoding Byte
$FileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$JSONObject = @"
{
"data" : "$filecontentencoded",
"dataType" : "pfx",
"password" : "$CertPassword"
}
"@
$JSONObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$JSONEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)
#Set the secret and set the key vault policy for -EnabledForDeployment
$Secret = ConvertTo-SecureString -String $JSONEncoded -AsPlainText -Force
Set-AzKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretName -SecretValue $Secret
Set-AzKeyVaultAccessPolicy -VaultName $KeyVaultName -ResourceGroupName $KVRGname -EnabledForDeployment
# Deploy the certificate to the VM
# Fill in 'MySecureVM' and 'MyVirtualMachineResourceGroup' with your values.
$VMName = 'MySecureVM'
$VMRGName = 'MyVirtualMachineResourceGroup'
$CertUrl = (Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretName).Id
$SourceVaultId = (Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGName).ResourceId
$VM = Get-AzVM -ResourceGroupName $VMRGName -Name $VMName
$VM = Add-AzVMSecret -VM $VM -SourceVaultId $SourceVaultId -CertificateStore "My" -CertificateUrl $CertUrl
Update-AzVM -VM $VM -ResourceGroupName $VMRGName
#Enable encryption on the VM by using Microsoft Entra client ID and the client certificate thumbprint
Set-AzVMDiskEncryptionExtension -ResourceGroupName $VMRGName -VMName $VMName -Aadclient ID $AADclient ID -AadClientCertThumbprint $AADClientCertThumbprint -DiskEncryptionKeyVaultUrl $DiskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId
憑證式驗證和 KEK (選擇性)
如果你想使用憑證認證並將加密金鑰包裝成 KEK,可以用以下腳本作為範例。 在使用 PowerShell 腳本之前,先複習所有 Azure 磁碟加密 的前置條件,以了解腳本中的步驟。 範例指令碼可能需要隨環境加以變更。
# Fill in 'MyKeyVaultResourceGroup', 'MySecureVault', and 'MyLocation' (if needed)
$KVRGname = 'MyKeyVaultResourceGroup'
$KeyVaultName= 'MySecureVault'
# Create a key vault and set enabledForDiskEncryption property on it.
# Comment out the next three lines if you already have an existing key vault enabled for encryption.
$Loc = 'MyLocation'
New-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname -Location $Loc
Set-AzKeyVaultAccessPolicy -VaultName $KeyVaultName -ResourceGroupName $KVRGname -EnabledForDiskEncryption
# Create the Microsoft Entra application and associate the certificate with it.
# Fill in "C:\certificates\mycert.pfx", "Password", "<My Application Display Name>", "<https://MyApplicationHomePage>", and "<https://MyApplicationUri>" with your values.
# MyApplicationHomePage and the MyApplicationUri can be any values you wish
$CertPath = "C:\certificates\mycert.pfx"
$CertPassword = "Password"
$Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath, $CertPassword)
$CertValue = [System.Convert]::ToBase64String($cert.GetRawCertData())
$AzureAdApplication = New-AzADApplication -DisplayName "<My Application Display Name>" -HomePage "<https://MyApplicationHomePage>" -IdentifierUris "<https://MyApplicationUri>" -CertValue $CertValue
$ServicePrincipal = New-AzADServicePrincipal -ApplicationId $AzureAdApplication.ApplicationId -Role Contributor
$AADclient ID = $AzureAdApplication.ApplicationId
$aadClientCertThumbprint= $cert.Thumbprint
## Give access for setting secrets and wrapping keys
Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -ServicePrincipalName $aadclient ID -PermissionsToKeys 'WrapKey' -PermissionsToSecrets 'Set' -ResourceGroupName $KVRGname
# Upload the pfx file to the key vault.
# Fill in "MyAADCert".
$KeyVaultSecretName = "MyAADCert"
$FileContentBytes = get-content $CertPath -Encoding Byte
$FileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$JSONObject = @"
{
"data" : "$filecontentencoded",
"dataType" : "pfx",
"password" : "$CertPassword"
}
"@
$JSONObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$JSONEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)
#Set the secret and set the key vault policy for deployment
$Secret = ConvertTo-SecureString -String $JSONEncoded -AsPlainText -Force
Set-AzKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretName -SecretValue $Secret
Set-AzKeyVaultAccessPolicy -VaultName $KeyVaultName -ResourceGroupName $KVRGname -EnabledForDeployment
#Setting some variables with the key vault information and generating a KEK
# Fill in 'KEKName'
$KEKName ='KEKName'
$KeyVault = Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGname
$DiskEncryptionKeyVaultUrl = $KeyVault.VaultUri
$KeyVaultResourceId = $KeyVault.ResourceId
$KEK = Add-AzKeyVaultKey -VaultName $KeyVaultName -Name $KEKName -Destination "Software"
$KeyEncryptionKeyUrl = $KEK.Key.kid
# Deploy the certificate to the VM
# Fill in 'MySecureVM' and 'MyVirtualMachineResourceGroup' with your values.
$VMName = 'MySecureVM';
$VMRGName = 'MyVirtualMachineResourceGroup';
$CertUrl = (Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretName).Id
$SourceVaultId = (Get-AzKeyVault -VaultName $KeyVaultName -ResourceGroupName $KVRGName).ResourceId
$VM = Get-AzVM -ResourceGroupName $VMRGName -Name $VMName
$VM = Add-AzVMSecret -VM $VM -SourceVaultId $SourceVaultId -CertificateStore "My" -CertificateUrl $CertUrl
Update-AzVM -VM $VM -ResourceGroupName $VMRGName
#Enable encryption on the VM by using Microsoft Entra client ID and the client certificate thumbprint
Set-AzVMDiskEncryptionExtension -ResourceGroupName $VMRGName -VMName $VMName -Aadclient ID $AADclient ID -AadClientCertThumbprint $AADClientCertThumbprint -DiskEncryptionKeyVaultUrl $DiskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId -KeyEncryptionKeyUrl $keyEncryptionKeyUrl -KeyEncryptionKeyVaultId $KeyVaultResourceId