|
13 | 13 | using System.IO;
|
14 | 14 | using System.Linq;
|
15 | 15 | using System.Management.Automation;
|
| 16 | +using System.Threading.Tasks; |
16 | 17 |
|
17 | 18 | namespace SharePointPnP.PowerShell.Commands.Provisioning.Tenant
|
18 | 19 | {
|
@@ -256,22 +257,85 @@ protected override void ExecuteCmdlet()
|
256 | 257 | }
|
257 | 258 | }
|
258 | 259 | }
|
259 |
| - if (!string.IsNullOrEmpty(SequenceId)) |
| 260 | +#if !ONPREMISES |
| 261 | + using (var provisioningContext = new PnPProvisioningContext((resource, scope) => |
260 | 262 | {
|
261 |
| - Tenant.ApplyProvisionHierarchy(hierarchyToApply, SequenceId, applyingInformation); |
262 |
| - } |
263 |
| - else |
| 263 | + // Get Azure AD Token |
| 264 | + if (AccessToken != null) |
| 265 | + { |
| 266 | + // Authenticated using -Graph or using another way to retrieve the accesstoken with Connect-PnPOnline |
| 267 | + return Task.FromResult(AccessToken); |
| 268 | + } |
| 269 | + else if (SPOnlineConnection.CurrentConnection.PSCredential != null) |
| 270 | + { |
| 271 | + // Using normal credentials |
| 272 | + return Task.FromResult(TokenHandler.AcquireToken(resource, null)); |
| 273 | + } |
| 274 | + else |
| 275 | + { |
| 276 | + // No token... |
| 277 | + return null; |
| 278 | + } |
| 279 | + })) |
264 | 280 | {
|
265 |
| - foreach (var sequence in hierarchyToApply.Sequences) |
| 281 | +#endif |
| 282 | + if (!string.IsNullOrEmpty(SequenceId)) |
266 | 283 | {
|
267 |
| - Tenant.ApplyProvisionHierarchy(hierarchyToApply, sequence.ID, applyingInformation); |
| 284 | + Tenant.ApplyProvisionHierarchy(hierarchyToApply, SequenceId, applyingInformation); |
268 | 285 | }
|
| 286 | + else |
| 287 | + { |
| 288 | + if (hierarchyToApply.Sequences.Count > 0) |
| 289 | + { |
| 290 | + foreach (var sequence in hierarchyToApply.Sequences) |
| 291 | + { |
| 292 | + Tenant.ApplyProvisionHierarchy(hierarchyToApply, sequence.ID, applyingInformation); |
| 293 | + } |
| 294 | + } |
| 295 | + else |
| 296 | + { |
| 297 | + Tenant.ApplyProvisionHierarchy(hierarchyToApply, null, applyingInformation); |
| 298 | + } |
| 299 | + } |
| 300 | +#if !ONPREMISES |
269 | 301 | }
|
270 |
| - |
| 302 | +#endif |
271 | 303 | WriteObject(sitesProvisioned, true);
|
272 | 304 |
|
273 | 305 | }
|
274 | 306 |
|
| 307 | + private string AccessToken |
| 308 | + { |
| 309 | + get |
| 310 | + { |
| 311 | + if (SPOnlineConnection.AuthenticationResult != null) |
| 312 | + { |
| 313 | + if (SPOnlineConnection.AuthenticationResult.ExpiresOn < DateTimeOffset.Now) |
| 314 | + { |
| 315 | + WriteWarning(Properties.Resources.MicrosoftGraphOAuthAccessTokenExpired); |
| 316 | + SPOnlineConnection.AuthenticationResult = null; |
| 317 | + return null; |
| 318 | + } |
| 319 | + else |
| 320 | + { |
| 321 | +#if !NETSTANDARD2_0 |
| 322 | + return (SPOnlineConnection.AuthenticationResult.Token); |
| 323 | +#else |
| 324 | + return SPOnlineConnection.AuthenticationResult.AccessToken; |
| 325 | +#endif |
| 326 | + } |
| 327 | + } |
| 328 | + else if (SPOnlineConnection.CurrentConnection?.AccessToken != null) |
| 329 | + { |
| 330 | + return SPOnlineConnection.CurrentConnection.AccessToken; |
| 331 | + } |
| 332 | + else |
| 333 | + { |
| 334 | + return null; |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + |
275 | 339 |
|
276 | 340 | private ProvisioningHierarchy GetHierarchy()
|
277 | 341 | {
|
|
0 commit comments