@@ -43,16 +43,13 @@ $ErrorActionPreference = "Stop"
43
43
# #######################################################################################################################
44
44
# Section1: Log-in to Azure and select appropriate subscription.
45
45
# #######################################################################################################################
46
-
47
46
48
47
Select-AzureRmSubscription - SubscriptionId $subscriptionId ;
49
48
50
-
51
49
# #######################################################################################################################
52
50
# Section2: Create AAD app . Fill in $aadClientSecret variable if AAD app was already created
53
51
# #######################################################################################################################
54
52
55
-
56
53
# Check if AAD app with $aadAppName was already created
57
54
$SvcPrincipals = (Get-AzureRmADServicePrincipal - SearchString $aadAppName );
58
55
if (-not $SvcPrincipals )
@@ -187,7 +184,36 @@ $ErrorActionPreference = "Stop"
187
184
Read-Host ;
188
185
189
186
# #######################################################################################################################
190
- # For each VM you want to encrypt, run the below cmdlet
191
- # $vmName = 'Name of VM to encrypt';
192
- # Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $resourceGroupName -VMName $vmName -AadClientID $aadClientID -AadClientSecret $aadClientSecret -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $keyVaultResourceId -VolumeType All
187
+ # To encrypt one VM in given resource group of the logged in subscritpion, assign $vmName and uncomment below section
188
+ # #######################################################################################################################
189
+ # $vmName = "Your VM Name";
190
+ # $allVMs = Get-AzureRmVm -ResourceGroupName $resourceGroupName -Name $vmName;
191
+
192
+ # #######################################################################################################################
193
+ # To encrypt all the VMs in the given resource group of the logged in subscription uncomment below section
193
194
# #######################################################################################################################
195
+ # $allVMs = Get-AzureRmVm -ResourceGroupName $resourceGroupName;
196
+
197
+ # #######################################################################################################################
198
+ # To encrypt all the VMs in the all the resource groups of the logged in subscription, uncomment below section
199
+ # #######################################################################################################################
200
+ # $allVMs = Get-AzureRmVm;
201
+
202
+ # #######################################################################################################################
203
+ # Loop through the selected list of VMs and enable encryption
204
+ # #######################################################################################################################
205
+
206
+ foreach ($vm in $allVMs )
207
+ {
208
+ Write-Host " Encrypting VM: $ ( $vm.Name ) in ResourceGroup: $ ( $vm.ResourceGroupName ) " - foregroundcolor Green;
209
+ if (-not $kek )
210
+ {
211
+ Set-AzureRmVMDiskEncryptionExtension - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name - AadClientID $aadClientID - AadClientSecret $aadClientSecret - DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl - DiskEncryptionKeyVaultId $keyVaultResourceId - VolumeType ' All' ;
212
+ }
213
+ else
214
+ {
215
+ Set-AzureRmVMDiskEncryptionExtension - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name - AadClientID $aadClientID - AadClientSecret $aadClientSecret - DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl - DiskEncryptionKeyVaultId $keyVaultResourceId - KeyEncryptionKeyUrl $keyEncryptionKeyUrl - KeyEncryptionKeyVaultId $keyVaultResourceId - VolumeType ' All' ;
216
+ }
217
+ # Show encryption status of the VM
218
+ Get-AzureRmVmDiskEncryptionStatus - ResourceGroupName $vm.ResourceGroupName - VMName $vm.Name ;
219
+ }
0 commit comments