@@ -151,6 +151,8 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
151
151
[ ValidateNotNullOrEmpty ]
152
152
public SwitchParameter Force { get ; set ; }
153
153
154
+ private string currentOSType = null ;
155
+
154
156
private void ValidateInputParameters ( )
155
157
{
156
158
if ( false == Uri . IsWellFormedUriString ( DiskEncryptionKeyVaultId , UriKind . Absolute ) )
@@ -192,24 +194,24 @@ private string GetExtensionStatusMessage()
192
194
ErrorCategory . InvalidResult ,
193
195
null ) ) ;
194
196
}
195
- bool publisherMismatch = false ;
197
+ bool publisherMatch = false ;
196
198
if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
197
199
{
198
200
if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
199
- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
201
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
200
202
{
201
- publisherMismatch = true ;
203
+ publisherMatch = true ;
202
204
}
203
205
}
204
- else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
206
+ else if ( string . Equals ( currentOSType , "Windows" , StringComparison . InvariantCultureIgnoreCase ) )
205
207
{
206
208
if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
207
- returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
209
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
208
210
{
209
- publisherMismatch = true ;
211
+ publisherMatch = true ;
210
212
}
211
213
}
212
- if ( publisherMismatch )
214
+ if ( publisherMatch )
213
215
{
214
216
AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
215
217
if ( ( context == null ) ||
@@ -305,13 +307,11 @@ private string GetExtensionProtectedSettings()
305
307
return JsonConvert . SerializeObject ( protectedSettings ) ;
306
308
}
307
309
308
- private VirtualMachineExtension GetVmExtensionParameters ( )
310
+ private VirtualMachineExtension GetVmExtensionParameters ( VirtualMachine vmParameters )
309
311
{
310
312
string SettingString = GetExtensionPublicSettings ( ) ;
311
313
string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
312
314
313
-
314
- VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
315
315
if ( vmParameters == null )
316
316
{
317
317
ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , "Set-AzureDiskEncryptionExtension can enable encryption only on a VM that was already created " ) ) ,
@@ -353,7 +353,29 @@ private VirtualMachineExtension GetVmExtensionParameters()
353
353
return vmExtensionParameters ;
354
354
}
355
355
356
- private string currentOSType = null ;
356
+ private void CreateVMBackupForLinx ( )
357
+ {
358
+ try
359
+ {
360
+ AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil ( ) ;
361
+ AzureVMBackupConfig vmConfig = new AzureVMBackupConfig ( ) ;
362
+ vmConfig . ResourceGroupName = ResourceGroupName ;
363
+ vmConfig . VMName = VMName ;
364
+ vmConfig . VirtualMachineExtensionType = VirtualMachineExtensionType ;
365
+ string tag = string . Format ( "{0}{1}" , "AzureEnc" , Guid . NewGuid ( ) . ToString ( ) ) ;
366
+ // this would create shapshot only for Linux box. and we should wait for the snapshot found.
367
+ azureBackupExtensionUtil . CreateSnapshotForDisks ( vmConfig , tag , this ) ;
368
+
369
+ WriteInformation ( new InformationRecord ( string . Format ( "one snapshot for disks are created with tag,{0}" , tag ) , string . Empty ) ) ;
370
+ }
371
+ catch ( AzureVMBackupException e )
372
+ {
373
+ ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , e . ToString ( ) ) ) ,
374
+ "InvalidResult" ,
375
+ ErrorCategory . InvalidResult ,
376
+ null ) ) ;
377
+ }
378
+ }
357
379
358
380
protected override void ProcessRecord ( )
359
381
{
@@ -368,37 +390,16 @@ protected override void ProcessRecord()
368
390
369
391
currentOSType = virtualMachineResponse . StorageProfile . OSDisk . OperatingSystemType ;
370
392
371
- if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
393
+ if ( string . Equals ( currentOSType , "Linux" , StringComparison . InvariantCultureIgnoreCase ) )
372
394
{
373
- try
374
- {
375
- AzureVMBackupExtensionUtil azureBackupExtensionUtil = new AzureVMBackupExtensionUtil ( ) ;
376
- AzureVMBackupConfig vmConfig = new AzureVMBackupConfig ( ) ;
377
- vmConfig . ResourceGroupName = ResourceGroupName ;
378
- vmConfig . VMName = VMName ;
379
- vmConfig . VirtualMachineExtensionType = VirtualMachineExtensionType ;
380
- string tag = string . Format ( "{0}{1}" , "AzureEnc" , Guid . NewGuid ( ) . ToString ( ) ) ;
381
- // this would create shapshot only for Linux box. and we should wait for the snapshot found.
382
- azureBackupExtensionUtil . CreateSnapshotForDisks ( vmConfig , tag , this ) ;
383
-
384
- WriteInformation ( new InformationRecord ( string . Format ( "one snapshot for disks are created with tag,{0}" , tag ) , string . Empty ) ) ;
385
- }
386
- catch ( AzureVMBackupException e )
387
- {
388
- ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , e . ToString ( ) ) ) ,
389
- "InvalidResult" ,
390
- ErrorCategory . InvalidResult ,
391
- null ) ) ;
392
- }
395
+ CreateVMBackupForLinx ( ) ;
393
396
}
394
397
395
- VirtualMachineExtension parameters = GetVmExtensionParameters ( ) ;
398
+ VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse ) ;
396
399
397
400
this . VirtualMachineExtensionClient . CreateOrUpdate ( this . ResourceGroupName ,
398
- this . VMName ,
399
- parameters ) ;
400
-
401
-
401
+ this . VMName ,
402
+ parameters ) ;
402
403
403
404
var op = UpdateVmEncryptionSettings ( ) ;
404
405
WriteObject ( Mapper . Map < PSComputeLongRunningOperation > ( op ) ) ;
0 commit comments