@@ -106,6 +106,49 @@ private VirtualMachineExtension GetVmExtensionParameters(VirtualMachine vmParame
106
106
return vmExtensionParameters ;
107
107
}
108
108
109
+ private bool IsExtensionInstalled ( OSType currentOSType )
110
+ {
111
+
112
+ AzureOperationResponse < VirtualMachineExtension > extensionResult = this . VirtualMachineExtensionClient . GetWithInstanceView ( this . ResourceGroupName , this . VMName , this . Name ) ;
113
+ if ( extensionResult == null )
114
+ {
115
+ return false ;
116
+ }
117
+
118
+ PSVirtualMachineExtension returnedExtension = extensionResult . ToPSVirtualMachineExtension (
119
+ this . ResourceGroupName , this . VMName ) ;
120
+
121
+ if ( ( returnedExtension == null ) ||
122
+ ( string . IsNullOrWhiteSpace ( returnedExtension . Publisher ) ) ||
123
+ ( string . IsNullOrWhiteSpace ( returnedExtension . ExtensionType ) ) )
124
+ {
125
+ return false ;
126
+ }
127
+ bool publisherMatch = false ;
128
+ if ( OSType . Linux . Equals ( currentOSType ) )
129
+ {
130
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
131
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
132
+ {
133
+ publisherMatch = true ;
134
+ }
135
+ }
136
+ else if ( OSType . Windows . Equals ( currentOSType ) )
137
+ {
138
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
139
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
140
+ {
141
+ publisherMatch = true ;
142
+ }
143
+ }
144
+ if ( publisherMatch )
145
+ {
146
+ return true ;
147
+ }
148
+
149
+ return false ;
150
+ }
151
+
109
152
private string GetExtensionStatusMessage ( OSType currentOSType , bool returnSubstatusMessage = false )
110
153
{
111
154
AzureOperationResponse < VirtualMachineExtension > extensionResult = this . VirtualMachineExtensionClient . GetWithInstanceView ( this . ResourceGroupName , this . VMName , this . Name ) ;
@@ -364,15 +407,18 @@ public override void ExecuteCmdlet()
364
407
WriteObject ( encryptionStatus ) ;
365
408
break ;
366
409
case OSType . Linux :
367
- VirtualMachine virtualMachineResponse = this . ComputeClient . ComputeManagementClient . VirtualMachines . GetWithInstanceView (
368
- this . ResourceGroupName , VMName ) . Body ;
369
- VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse , osType ) ;
370
-
371
- this . VirtualMachineExtensionClient . CreateOrUpdateWithHttpMessagesAsync (
372
- this . ResourceGroupName ,
373
- this . VMName ,
374
- this . Name ,
375
- parameters ) . GetAwaiter ( ) . GetResult ( ) ;
410
+ if ( ! IsExtensionInstalled ( osType ) )
411
+ {
412
+ VirtualMachine virtualMachineResponse = this . ComputeClient . ComputeManagementClient . VirtualMachines . GetWithInstanceView (
413
+ this . ResourceGroupName , VMName ) . Body ;
414
+ VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse , osType ) ;
415
+
416
+ this . VirtualMachineExtensionClient . CreateOrUpdateWithHttpMessagesAsync (
417
+ this . ResourceGroupName ,
418
+ this . VMName ,
419
+ this . Name ,
420
+ parameters ) . GetAwaiter ( ) . GetResult ( ) ;
421
+ }
376
422
377
423
Dictionary < string , string > encryptionStatusParsed = null ;
378
424
try
0 commit comments