@@ -106,6 +106,57 @@ private VirtualMachineExtension GetVmExtensionParameters(VirtualMachine vmParame
106
106
return vmExtensionParameters ;
107
107
}
108
108
109
+ private bool IsExtensionInstalled ( OSType currentOSType )
110
+ {
111
+ if ( OSType . Windows . Equals ( currentOSType ) )
112
+ {
113
+ this . Name = this . Name ?? AzureDiskEncryptionExtensionContext . ExtensionDefaultName ;
114
+ }
115
+ else if ( OSType . Linux . Equals ( currentOSType ) )
116
+ {
117
+ this . Name = this . Name ?? AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName ;
118
+ }
119
+
120
+ AzureOperationResponse < VirtualMachineExtension > extensionResult = this . VirtualMachineExtensionClient . GetWithInstanceView ( this . ResourceGroupName , this . VMName , this . Name ) ;
121
+ if ( extensionResult == null )
122
+ {
123
+ return false ;
124
+ }
125
+
126
+ PSVirtualMachineExtension returnedExtension = extensionResult . ToPSVirtualMachineExtension (
127
+ this . ResourceGroupName , this . VMName ) ;
128
+
129
+ if ( ( returnedExtension == null ) ||
130
+ ( string . IsNullOrWhiteSpace ( returnedExtension . Publisher ) ) ||
131
+ ( string . IsNullOrWhiteSpace ( returnedExtension . ExtensionType ) ) )
132
+ {
133
+ return false ;
134
+ }
135
+ bool publisherMatch = false ;
136
+ if ( OSType . Linux . Equals ( currentOSType ) )
137
+ {
138
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
139
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
140
+ {
141
+ publisherMatch = true ;
142
+ }
143
+ }
144
+ else if ( OSType . Windows . Equals ( currentOSType ) )
145
+ {
146
+ if ( returnedExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) &&
147
+ returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
148
+ {
149
+ publisherMatch = true ;
150
+ }
151
+ }
152
+ if ( publisherMatch )
153
+ {
154
+ return true ;
155
+ }
156
+
157
+ return false ;
158
+ }
159
+
109
160
private string GetExtensionStatusMessage ( OSType currentOSType , bool returnSubstatusMessage = false )
110
161
{
111
162
AzureOperationResponse < VirtualMachineExtension > extensionResult = this . VirtualMachineExtensionClient . GetWithInstanceView ( this . ResourceGroupName , this . VMName , this . Name ) ;
@@ -364,15 +415,18 @@ public override void ExecuteCmdlet()
364
415
WriteObject ( encryptionStatus ) ;
365
416
break ;
366
417
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 ( ) ;
418
+ if ( ! IsExtensionInstalled ( osType ) )
419
+ {
420
+ VirtualMachine virtualMachineResponse = this . ComputeClient . ComputeManagementClient . VirtualMachines . GetWithInstanceView (
421
+ this . ResourceGroupName , VMName ) . Body ;
422
+ VirtualMachineExtension parameters = GetVmExtensionParameters ( virtualMachineResponse , osType ) ;
423
+
424
+ this . VirtualMachineExtensionClient . CreateOrUpdateWithHttpMessagesAsync (
425
+ this . ResourceGroupName ,
426
+ this . VMName ,
427
+ this . Name ,
428
+ parameters ) . GetAwaiter ( ) . GetResult ( ) ;
429
+ }
376
430
377
431
Dictionary < string , string > encryptionStatusParsed = null ;
378
432
try
0 commit comments