@@ -65,10 +65,19 @@ public class GetAzureRmRecoveryServicesVaultSettingsFile : RecoveryServicesCmdle
65
65
[ ValidateNotNullOrEmpty ]
66
66
public ARSVault Vault { get ; set ; }
67
67
68
+ /// <summary>
69
+ /// Gets or sets the path where the credential file is to be generated
70
+ /// </summary>
71
+ /// <summary>
72
+ /// Gets or sets vault Object.
73
+ /// </summary>
74
+ [ Parameter ( Position = 2 ) ]
75
+ public string Path { get ; set ; }
76
+
77
+ #if NETSTANDARD
68
78
/// <summary>
69
79
/// Gets or sets Site Identifier.
70
80
/// </summary>
71
- [ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = true ) ]
72
81
[ Parameter ( ParameterSetName = ARSParameterSets . ForSiteWithCertificate , Mandatory = true ) ]
73
82
[ ValidateNotNullOrEmpty ]
74
83
public string SiteIdentifier { get ; set ; }
@@ -85,7 +94,6 @@ public class GetAzureRmRecoveryServicesVaultSettingsFile : RecoveryServicesCmdle
85
94
/// <summary>
86
95
/// Gets or sets SiteFriendlyName.
87
96
/// </summary>
88
- [ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = true ) ]
89
97
[ Parameter ( ParameterSetName = ARSParameterSets . ForSiteWithCertificate , Mandatory = true ) ]
90
98
[ ValidateNotNullOrEmpty ]
91
99
public string SiteFriendlyName { get ; set ; }
@@ -96,8 +104,48 @@ public class GetAzureRmRecoveryServicesVaultSettingsFile : RecoveryServicesCmdle
96
104
/// <summary>
97
105
/// Gets or sets vault Object.
98
106
/// </summary>
99
- [ Parameter ( Position = 2 ) ]
100
- public string Path { get ; set ; }
107
+ [ Parameter ( ParameterSetName = ARSParameterSets . ByDefaultWithCertificate , Mandatory = false ) ]
108
+ [ Parameter ( ParameterSetName = ARSParameterSets . ForSiteWithCertificate , Mandatory = false ) ]
109
+ public SwitchParameter SiteRecovery
110
+ {
111
+ get { return siteRecovery ; }
112
+ set { siteRecovery = value ; }
113
+ }
114
+
115
+ /// <summary>
116
+ /// Gets or sets the path where the credential file is to be generated
117
+ /// </summary>
118
+ /// <summary>
119
+ /// Gets or sets vault Object.
120
+ /// </summary>
121
+ [ Parameter ( ParameterSetName = ARSParameterSets . ForBackupVaultTypeWithCertificate , Mandatory = true ) ]
122
+ public SwitchParameter Backup
123
+ {
124
+ get { return backup ; }
125
+ set { backup = value ; }
126
+ }
127
+ #else
128
+ /// <summary>
129
+ /// Gets or sets Site Identifier.
130
+ /// </summary>
131
+ [ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = true ) ]
132
+ [ ValidateNotNullOrEmpty ]
133
+ public string SiteIdentifier { get ; set ; }
134
+
135
+ /// <summary>
136
+ /// Gets or sets certificate.
137
+ /// </summary>
138
+ [ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = true ) ]
139
+ [ Parameter ( ParameterSetName = ARSParameterSets . ByDefault , Mandatory = true ) ]
140
+ [ ValidateNotNullOrEmpty ]
141
+ public string certificate { get ; set ; }
142
+
143
+ /// <summary>
144
+ /// Gets or sets SiteFriendlyName.
145
+ /// </summary>
146
+ [ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = true ) ]
147
+ [ ValidateNotNullOrEmpty ]
148
+ public string SiteFriendlyName { get ; set ; }
101
149
102
150
/// <summary>
103
151
/// Gets or sets the path where the credential file is to be generated
@@ -107,14 +155,11 @@ public class GetAzureRmRecoveryServicesVaultSettingsFile : RecoveryServicesCmdle
107
155
/// </summary>
108
156
[ Parameter ( ParameterSetName = ARSParameterSets . ByDefault , Mandatory = false ) ]
109
157
[ Parameter ( ParameterSetName = ARSParameterSets . ForSite , Mandatory = false ) ]
110
- [ Parameter ( ParameterSetName = ARSParameterSets . ByDefaultWithCertificate , Mandatory = false ) ]
111
- [ Parameter ( ParameterSetName = ARSParameterSets . ForSiteWithCertificate , Mandatory = false ) ]
112
158
public SwitchParameter SiteRecovery
113
159
{
114
160
get { return siteRecovery ; }
115
161
set { siteRecovery = value ; }
116
162
}
117
- private bool siteRecovery ;
118
163
119
164
/// <summary>
120
165
/// Gets or sets the path where the credential file is to be generated
@@ -123,12 +168,14 @@ public SwitchParameter SiteRecovery
123
168
/// Gets or sets vault Object.
124
169
/// </summary>
125
170
[ Parameter ( ParameterSetName = ARSParameterSets . ForBackupVaultType , Mandatory = true ) ]
126
- [ Parameter ( ParameterSetName = ARSParameterSets . ForBackupVaultTypeWithCertificate , Mandatory = true ) ]
127
171
public SwitchParameter Backup
128
172
{
129
173
get { return backup ; }
130
174
set { backup = value ; }
131
175
}
176
+ #endif
177
+
178
+ private bool siteRecovery ;
132
179
private bool backup ;
133
180
134
181
#endregion Parameters
@@ -140,8 +187,7 @@ public override void ExecuteCmdlet()
140
187
{
141
188
try
142
189
{
143
- if ( this . MyInvocation . BoundParameters . ContainsKey ( "this.certificate" ) )
144
- {
190
+ #if NETSTANDARD
145
191
if ( backup )
146
192
{
147
193
this . GetBackupCredentialsWithCertificate ( certificate ) ;
@@ -150,23 +196,19 @@ public override void ExecuteCmdlet()
150
196
{
151
197
this . GetSiteRecoveryCredentialsWithCertificate ( certificate ) ;
152
198
}
199
+
200
+ #else
201
+
202
+ if ( backup )
203
+ {
204
+ this . GetAzureRMRecoveryServicesVaultBackupCredentials ( ) ;
153
205
}
154
206
else
155
207
{
156
- #if ! NETSTANDARD
157
- if ( backup )
158
- {
159
- this . GetAzureRMRecoveryServicesVaultBackupCredentials ( ) ;
160
- }
161
- else
162
- {
163
- this . GetSiteRecoveryCredentials ( ) ;
164
- }
165
- #else
166
- throw new ArgumentException ( Resources . MissingCertificateForNetStandard ) ;
167
- #endif
208
+ this . GetSiteRecoveryCredentials ( ) ;
168
209
}
169
210
211
+ #endif
170
212
}
171
213
catch ( AggregateException aggregateEx )
172
214
{
@@ -254,7 +296,64 @@ private void GetBackupCredentialsWithCertificate(string certificate)
254
296
255
297
private void GetSiteRecoveryCredentialsWithCertificate ( string certificate )
256
298
{
299
+ string subscriptionId = DefaultContext . Subscription . Id . ToString ( ) ;
300
+ VaultCertificateResponse vaultCertificateResponse = null ;
301
+ ASRSite site = new ASRSite ( ) ;
302
+
303
+ if ( ! string . IsNullOrEmpty ( this . SiteIdentifier )
304
+ && ! string . IsNullOrEmpty ( this . SiteFriendlyName ) )
305
+ {
306
+ site . ID = this . SiteIdentifier ;
307
+ site . Name = this . SiteFriendlyName ;
308
+ }
309
+ try
310
+ {
311
+ string fileName = this . GenerateFileName ( ) ;
312
+
313
+ string filePath = string . IsNullOrEmpty ( this . Path ) ? Utilities . GetDefaultPath ( ) : this . Path ;
314
+ string fullFilePath = System . IO . Path . Combine ( filePath , fileName ) ;
315
+ // Upload cert into ID Mgmt
316
+ WriteDebug ( string . Format ( CultureInfo . InvariantCulture , Resources . UploadingCertToIdmgmt ) ) ;
317
+ byte [ ] bytes = Encoding . ASCII . GetBytes ( certificate ) ;
318
+ var certificateArgs = new CertificateRequest ( ) ;
319
+ certificateArgs . Properties = new RawCertificateData ( ) ;
320
+ certificateArgs . Properties . Certificate = bytes ;
321
+ certificateArgs . Properties . AuthType = AuthType . AAD ;
322
+
323
+
324
+ string dateString = DateTime . Now . ToString ( "M-d-yyyy" ) ;
325
+
326
+ var friendlyName = string . Format ( "{0}{1}-{2}-vaultcredentials" , this . Vault . Name , subscriptionId , dateString ) ;
327
+ vaultCertificateResponse = RecoveryServicesClient . GetRecoveryServicesClient . VaultCertificates . CreateWithHttpMessagesAsync (
328
+ this . Vault . ResourceGroupName ,
329
+ this . Vault . Name ,
330
+ friendlyName ,
331
+ certificateArgs ,
332
+ RecoveryServicesClient . GetRequestHeaders ( ) ) . Result . Body ;
333
+ WriteDebug ( string . Format ( CultureInfo . InvariantCulture , Resources . UploadedCertToIdmgmt ) ) ;
334
+
335
+ string vaultCredsFileContent = GenerateVaultCredsForSiteRecovery (
336
+ certificate ,
337
+ subscriptionId ,
338
+ vaultCertificateResponse ,
339
+ site ) ;
257
340
341
+ WriteDebug ( string . Format ( Resources . SavingVaultCred , fullFilePath ) ) ;
342
+
343
+ AzureSession . Instance . DataStore . WriteFile ( fullFilePath , Encoding . UTF8 . GetBytes ( vaultCredsFileContent ) ) ;
344
+
345
+ VaultSettingsFilePath output = new VaultSettingsFilePath ( )
346
+ {
347
+ FilePath = fullFilePath ,
348
+ } ;
349
+
350
+ // Output filename back to user
351
+ WriteObject ( output , true ) ;
352
+ }
353
+ catch ( Exception exception )
354
+ {
355
+ throw exception ;
356
+ }
258
357
}
259
358
260
359
/// <summary>
@@ -282,6 +381,7 @@ private void GetSiteRecoveryCredentials()
282
381
string fileName = this . GenerateFileName ( ) ;
283
382
284
383
string filePath = string . IsNullOrEmpty ( this . Path ) ? Utilities . GetDefaultPath ( ) : this . Path ;
384
+ filePath = ResolveUserPath ( filePath ) ;
285
385
286
386
// Generate file.
287
387
if ( RecoveryServicesClient . getVaultAuthType ( this . Vault . ResourceGroupName , this . Vault . Name ) == 0 )
@@ -376,6 +476,7 @@ private string GenerateFileName()
376
476
public void GetAzureRMRecoveryServicesVaultBackupCredentials ( )
377
477
{
378
478
string targetLocation = string . IsNullOrEmpty ( this . Path ) ? Utilities . GetDefaultPath ( ) : this . Path ;
479
+ targetLocation = ResolveUserPath ( targetLocation ) ;
379
480
if ( ! Directory . Exists ( targetLocation ) )
380
481
{
381
482
throw new ArgumentException ( Resources . VaultCredPathException ) ;
0 commit comments