@@ -24,6 +24,7 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
24
24
25
25
private const string aadClientIDKey = "AADClientID" ;
26
26
private const string aadClientSecretKey = "AADClientSecret" ;
27
+ private const string aadClientCertThumbprintKey = "AADClientCertThumbprint" ;
27
28
private const string keyVaultUrlKey = "KeyVaultURL" ;
28
29
private const string keyEncryptionKeyUrlKey = "KeyEncryptionKeyURL" ;
29
30
private const string keyEncryptionAlgorithmKey = "KeyEncryptionAlgorithm" ;
@@ -96,49 +97,41 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
96
97
HelpMessage = "ResourceID of the KeyVault where generated encryption key will be placed to" ) ]
97
98
public string DiskEncryptionKeyVaultId { get ; set ; }
98
99
99
- [ Parameter (
100
- Mandatory = true ,
101
- Position = 7 ,
102
- ValueFromPipelineByPropertyName = true ,
103
- HelpMessage = "The location of the VM" ) ]
104
- [ ValidateNotNullOrEmpty ]
105
- public string Location { get ; set ; }
106
-
107
100
[ Parameter (
108
101
Mandatory = false ,
109
- Position = 8 ,
102
+ Position = 7 ,
110
103
ValueFromPipelineByPropertyName = true ,
111
- HelpMessage = "KeyVault URL of the KeyEncryptionKey used to encrypt the disk encryption key" ) ]
104
+ HelpMessage = "Versioned KeyVault URL of the KeyEncryptionKey used to encrypt the disk encryption key" ) ]
112
105
[ ValidateNotNullOrEmpty ]
113
106
public string KeyEncryptionKeyUrl { get ; set ; }
114
107
115
108
[ Parameter (
116
109
Mandatory = false ,
117
- Position = 9 ,
110
+ Position = 8 ,
118
111
ValueFromPipelineByPropertyName = true ,
119
112
HelpMessage = "ResourceID of the KeyVault containing the KeyEncryptionKey used to encrypt the disk encryption key" ) ]
120
113
[ ValidateNotNullOrEmpty ]
121
114
public string KeyEncryptionKeyVaultId { get ; set ; }
122
115
123
116
[ Parameter (
124
117
Mandatory = false ,
125
- Position = 10 ,
118
+ Position = 9 ,
126
119
ValueFromPipelineByPropertyName = true ,
127
120
HelpMessage = "KeyEncryption Algorithm used to encrypt the volume encryption key" ) ]
128
121
[ ValidateSet ( "RSA-OAEP" , "RSA1_5" ) ]
129
122
public string KeyEncryptionAlgorithm { get ; set ; }
130
123
131
124
[ Parameter (
132
125
Mandatory = false ,
133
- Position = 11 ,
126
+ Position = 10 ,
134
127
ValueFromPipelineByPropertyName = true ,
135
128
HelpMessage = "Type of the volume (OS or Data) to perform encryption operation" ) ]
136
129
[ ValidateSet ( "OS" , "Data" , "All" ) ]
137
130
public string VolumeType { get ; set ; }
138
131
139
132
[ Parameter (
140
133
Mandatory = false ,
141
- Position = 12 ,
134
+ Position = 11 ,
142
135
ValueFromPipelineByPropertyName = true ,
143
136
HelpMessage = "Sequence version of encryption operation. This must be incremented to perform repeated encryption operations on the same VM" ) ]
144
137
[ ValidateNotNullOrEmpty ]
@@ -147,7 +140,7 @@ public class SetAzureDiskEncryptionExtensionCommand : VirtualMachineExtensionBas
147
140
[ Alias ( "HandlerVersion" , "Version" ) ]
148
141
[ Parameter (
149
142
Mandatory = false ,
150
- Position = 13 ,
143
+ Position = 12 ,
151
144
ValueFromPipelineByPropertyName = true ,
152
145
HelpMessage = "The type handler version." ) ]
153
146
[ ValidateNotNullOrEmpty ]
@@ -181,15 +174,15 @@ private void ValidateInputParameters()
181
174
private string GetExtensionStatusMessage ( )
182
175
{
183
176
VirtualMachineExtensionGetResponse extensionResult = this . VirtualMachineExtensionClient . GetWithInstanceView ( this . ResourceGroupName , this . VMName , this . Name ) ;
184
- if ( extensionResult == null )
177
+ if ( extensionResult == null )
185
178
{
186
179
ThrowTerminatingError ( new ErrorRecord ( new ApplicationFailedException ( string . Format ( CultureInfo . CurrentUICulture , "Failed to retrieve extension status" ) ) ,
187
180
"InvalidResult" ,
188
181
ErrorCategory . InvalidResult ,
189
182
null ) ) ;
190
183
}
191
184
PSVirtualMachineExtension returnedExtension = extensionResult . ToPSVirtualMachineExtension ( this . ResourceGroupName ) ;
192
- if ( ( returnedExtension == null ) ||
185
+ if ( ( returnedExtension == null ) ||
193
186
( string . IsNullOrWhiteSpace ( returnedExtension . Publisher ) ) ||
194
187
( string . IsNullOrWhiteSpace ( returnedExtension . ExtensionType ) ) )
195
188
{
@@ -202,9 +195,9 @@ private string GetExtensionStatusMessage()
202
195
returnedExtension . ExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) )
203
196
{
204
197
AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext ( returnedExtension ) ;
205
- if ( ( context == null ) ||
206
- ( context . Statuses == null ) ||
207
- ( context . Statuses . Count < 1 ) ||
198
+ if ( ( context == null ) ||
199
+ ( context . Statuses == null ) ||
200
+ ( context . Statuses . Count < 1 ) ||
208
201
( string . IsNullOrWhiteSpace ( context . Statuses [ 0 ] . Message ) ) )
209
202
{
210
203
ThrowTerminatingError ( new ErrorRecord ( new ApplicationFailedException ( string . Format ( CultureInfo . CurrentUICulture , "Invalid extension status" ) ) ,
@@ -248,7 +241,7 @@ private ComputeLongRunningOperationResponse UpdateVmEncryptionSettings()
248
241
encryptionSettings . DiskEncryptionKey . SourceVault = new SourceVaultReference ( ) ;
249
242
encryptionSettings . DiskEncryptionKey . SourceVault . ReferenceUri = this . DiskEncryptionKeyVaultId ;
250
243
encryptionSettings . DiskEncryptionKey . SecretUrl = statusMessage ;
251
- if ( this . KeyEncryptionKeyUrl != null )
244
+ if ( this . KeyEncryptionKeyUrl != null )
252
245
{
253
246
encryptionSettings . KeyEncryptionKey = new KeyVaultKeyReference ( ) ;
254
247
encryptionSettings . KeyEncryptionKey . SourceVault = new SourceVaultReference ( ) ;
@@ -276,6 +269,7 @@ private string GetExtensionPublicSettings()
276
269
{
277
270
Hashtable publicSettings = new Hashtable ( ) ;
278
271
publicSettings . Add ( aadClientIDKey , AadClientID ?? String . Empty ) ;
272
+ publicSettings . Add ( aadClientCertThumbprintKey , AadClientCertThumbprint ?? String . Empty ) ;
279
273
publicSettings . Add ( keyVaultUrlKey , DiskEncryptionKeyVaultUrl ?? String . Empty ) ;
280
274
publicSettings . Add ( keyEncryptionKeyUrlKey , KeyEncryptionKeyUrl ?? String . Empty ) ;
281
275
publicSettings . Add ( keyEncryptionAlgorithmKey , KeyEncryptionAlgorithm ?? String . Empty ) ;
@@ -299,9 +293,18 @@ private VirtualMachineExtension GetVmExtensionParameters()
299
293
string SettingString = GetExtensionPublicSettings ( ) ;
300
294
string ProtectedSettingString = GetExtensionProtectedSettings ( ) ;
301
295
296
+ VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) . VirtualMachine ;
297
+ if ( vmParameters == null )
298
+ {
299
+ ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , "Set-AzureDiskEncryptionExtension can enable encryption only on a VM that was already created " ) ) ,
300
+ "InvalidResult" ,
301
+ ErrorCategory . InvalidResult ,
302
+ null ) ) ;
303
+ }
304
+
302
305
VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension
303
306
{
304
- Location = this . Location ,
307
+ Location = vmParameters . Location ,
305
308
Name = this . Name ,
306
309
Type = VirtualMachineExtensionType ,
307
310
Publisher = AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher ,
0 commit comments