@@ -314,6 +314,11 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
314
314
ErrorCategory . InvalidResult ,
315
315
null ) ) ;
316
316
}
317
+ //encryption settings object to clear out encryption settings before updating
318
+ DiskEncryptionSettings resetEncryptionSettings = new DiskEncryptionSettings ( ) ;
319
+ resetEncryptionSettings . Enabled = false ;
320
+ resetEncryptionSettings . DiskEncryptionKey = null ;
321
+ resetEncryptionSettings . KeyEncryptionKey = null ;
317
322
318
323
DiskEncryptionSettings encryptionSettings = new DiskEncryptionSettings ( ) ;
319
324
encryptionSettings . Enabled = true ;
@@ -326,6 +331,7 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
326
331
encryptionSettings . KeyEncryptionKey . SourceVault = new SubResource ( this . KeyEncryptionKeyVaultId ) ;
327
332
encryptionSettings . KeyEncryptionKey . KeyUrl = this . KeyEncryptionKeyUrl ;
328
333
}
334
+
329
335
vmParameters . StorageProfile . OsDisk . EncryptionSettings = encryptionSettings ;
330
336
var parameters = new VirtualMachine
331
337
{
@@ -352,14 +358,35 @@ private AzureOperationResponse<VirtualMachine> UpdateVmEncryptionSettings(DiskEn
352
358
}
353
359
else
354
360
{
355
-
356
- // stop-update-start
361
+ // stop-clear-update-start
357
362
// stop vm
358
363
this . ComputeClient . ComputeManagementClient . VirtualMachines
359
364
. DeallocateWithHttpMessagesAsync ( this . ResourceGroupName , this . VMName ) . GetAwaiter ( )
360
365
. GetResult ( ) ;
361
366
362
- // update vm
367
+ // first update vm call to clear encryption settings
368
+ vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get (
369
+ this . ResourceGroupName , this . VMName ) ) ;
370
+ vmParameters . StorageProfile . OsDisk . EncryptionSettings = resetEncryptionSettings ;
371
+ parameters = new VirtualMachine
372
+ {
373
+ DiagnosticsProfile = vmParameters . DiagnosticsProfile ,
374
+ HardwareProfile = vmParameters . HardwareProfile ,
375
+ StorageProfile = vmParameters . StorageProfile ,
376
+ NetworkProfile = vmParameters . NetworkProfile ,
377
+ OsProfile = vmParameters . OsProfile ,
378
+ Plan = vmParameters . Plan ,
379
+ AvailabilitySet = vmParameters . AvailabilitySet ,
380
+ Location = vmParameters . Location ,
381
+ Tags = vmParameters . Tags
382
+ } ;
383
+
384
+ updateResult = this . ComputeClient . ComputeManagementClient . VirtualMachines . CreateOrUpdateWithHttpMessagesAsync (
385
+ this . ResourceGroupName ,
386
+ vmParameters . Name ,
387
+ parameters ) . GetAwaiter ( ) . GetResult ( ) ;
388
+
389
+ // second update vm call to set new encryption settings
363
390
vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get (
364
391
this . ResourceGroupName , this . VMName ) ) ;
365
392
vmParameters . StorageProfile . OsDisk . EncryptionSettings = encryptionSettings ;
@@ -566,23 +593,23 @@ public override void ExecuteCmdlet()
566
593
new DiskEncryptionSettings { Enabled = false } ;
567
594
568
595
// Single Pass
569
- // newer model, supported by newer extension versions and host functionality
596
+ // newer model, supported by newer extension versions and host functionality
570
597
// if SinglePassParameterSet is used, cmdlet will default to newer extension version
571
598
// [first and only pass]
572
599
// only one enable extension call will be issued from the cmdlet n
573
- // No AD identity information or protected settings will be passed to the extension
574
- // Host performs the necessary key vault operations and vm model updates
600
+ // No AD identity information or protected settings will be passed to the extension
601
+ // Host performs the necessary key vault operations and vm model updates
575
602
// Dual Pass
576
- // older model, supported by older extension versions
603
+ // older model, supported by older extension versions
577
604
// if an AD ParameterSet is used, cmdlet will default to older extension version
578
- // [first pass]
605
+ // [first pass]
579
606
// AD identity information is passed into the VM via protected settings of the extension
580
- // VM uses the AD identity to authenticate and perform key vault operations
607
+ // VM uses the AD identity to authenticate and perform key vault operations
581
608
// VM returns result of key vault operation to caller via the extension status message
582
609
// [second pass]
583
610
// powershell reads extension status message returned from first pass
584
611
// updates VM model with encryption settings
585
- // updates VM
612
+ // updates VM
586
613
587
614
// First Pass
588
615
AzureOperationResponse < VirtualMachineExtension > firstPass = this . VirtualMachineExtensionClient . CreateOrUpdateWithHttpMessagesAsync (
@@ -608,7 +635,7 @@ public override void ExecuteCmdlet()
608
635
}
609
636
else
610
637
{
611
- // Second pass
638
+ // Second pass
612
639
var secondPass = UpdateVmEncryptionSettings ( encryptionSettingsBackup ) ;
613
640
WriteObject ( ComputeAutoMapperProfile . Mapper . Map < PSAzureOperationResponse > ( secondPass ) ) ;
614
641
}
0 commit comments