-
Notifications
You must be signed in to change notification settings - Fork 9
/
AzureRMVMDiskEncryption.ps1
50 lines (38 loc) · 1.89 KB
/
AzureRMVMDiskEncryption.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$rgName = "azurechamp-edisk-rg"
$location = "West Europe"
Register-AzureRmResourceProvider -ProviderNamespace "Microsoft.KeyVault"
New-AzureRmResourceGroup -Location $location -Name $rgName
$keyVaultName = "psKeyVault2019"
New-AzureRmKeyVault -Location $location `
-ResourceGroupName $rgName `
-VaultName $keyVaultName `
-EnabledForDiskEncryption
Add-AzureKeyVaultKey -VaultName $keyVaultName `
-Name "ADEKEY" `
-Destination "Software"
$appName = "ADE-APP"
$securePassword = ConvertTo-SecureString -String "2019AzureChamp" -AsPlainText -Force
$app = New-AzureRmADApplication -DisplayName $appName `
-HomePage "https://acade.ps.local" `
-IdentifierUris "https://acade.ps/mac" `
-Password $securePassword
New-AzureRmADServicePrincipal -ApplicationId $app.ApplicationId
Set-AzureRmKeyVaultAccessPolicy -VaultName $keyvaultName `
-ServicePrincipalName $app.ApplicationId `
-PermissionsToKeys "WrapKey" `
-PermissionsToSecrets "Set"
$keyVault = Get-AzureRmKeyVault -VaultName $keyVaultName -ResourceGroupName $rgName;
$diskEncryptionKeyVaultUrl = $keyVault.VaultUri;
$keyVaultResourceId = $keyVault.ResourceId;
$keyEncryptionKeyUrl = (Get-AzureKeyVaultKey -VaultName $keyVaultName -Name ADEKEY).Key.kid
Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $rgName `
-VMName "managedserver" `
-AadClientID $app.ApplicationId `
-AadClientSecret (New-Object PSCredential "onur.yuksektepeli@yuksektek.com", $securePassword).GetNetworkCredential().Password `
-DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl `
-DiskEncryptionKeyVaultId $keyVaultResourceId `
-KeyEncryptionKeyUrl $keyEncryptionKeyUrl `
-KeyEncryptionKeyVaultId $keyVaultResourceId
Get-AzureRmVmDiskEncryptionStatus -ResourceGroupName $rgName -VMName "managedserver"
#Disable Disk Encryption
Disable-AzVMDiskEncryption -ResourceGroupName 'MyVirtualMachineResourceGroup' -VMName 'MySecureVM'