@@ -75,6 +75,16 @@ public class ConnectOnline : BasePSCmdlet
75
75
[ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE , ValueFromPipeline = true ) ]
76
76
public string Url ;
77
77
78
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_CREDENTIALS ) ]
79
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_ACSAPPONLY ) ]
80
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_APPONLYAADCERTIFICATE ) ]
81
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_APPONLYAADTHUMBPRINT ) ]
82
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_SPOMANAGEMENT ) ]
83
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_DEVICELOGIN ) ]
84
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_INTERACTIVE ) ]
85
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE ) ]
86
+ public PnPConnection Connection = PnPConnection . Current ;
87
+
78
88
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_CREDENTIALS ) ]
79
89
[ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_SPOMANAGEMENT ) ]
80
90
public CredentialPipeBind Credentials ;
@@ -237,7 +247,7 @@ protected void Connect(ref CancellationToken cancellationToken)
237
247
}
238
248
}
239
249
240
- PnPConnection connection = null ;
250
+ PnPConnection newConnection = null ;
241
251
242
252
PSCredential credentials = null ;
243
253
if ( Credentials != null )
@@ -249,42 +259,42 @@ protected void Connect(ref CancellationToken cancellationToken)
249
259
switch ( ParameterSetName )
250
260
{
251
261
case ParameterSet_SPOMANAGEMENT :
252
- connection = ConnectSpoManagement ( ) ;
262
+ newConnection = ConnectSpoManagement ( ) ;
253
263
break ;
254
264
case ParameterSet_DEVICELOGIN :
255
- connection = ConnectDeviceLogin ( ) ;
265
+ newConnection = ConnectDeviceLogin ( ) ;
256
266
break ;
257
267
case ParameterSet_APPONLYAADCERTIFICATE :
258
- connection = ConnectAppOnlyWithCertificate ( ) ;
268
+ newConnection = ConnectAppOnlyWithCertificate ( ) ;
259
269
break ;
260
270
case ParameterSet_APPONLYAADTHUMBPRINT :
261
- connection = ConnectAppOnlyWithCertificate ( ) ;
271
+ newConnection = ConnectAppOnlyWithCertificate ( ) ;
262
272
break ;
263
273
case ParameterSet_ACCESSTOKEN :
264
- connection = ConnectAccessToken ( ) ;
274
+ newConnection = ConnectAccessToken ( ) ;
265
275
break ;
266
276
case ParameterSet_ACSAPPONLY :
267
- connection = ConnectACSAppOnly ( ) ;
277
+ newConnection = ConnectACSAppOnly ( ) ;
268
278
break ;
269
279
case ParameterSet_CREDENTIALS :
270
- connection = ConnectCredentials ( credentials ) ;
280
+ newConnection = ConnectCredentials ( credentials ) ;
271
281
break ;
272
282
case ParameterSet_MANAGEDIDENTITY :
273
- connection = ConnectManagedIdentity ( ) ;
283
+ newConnection = ConnectManagedIdentity ( ) ;
274
284
break ;
275
285
case ParameterSet_WEBLOGIN :
276
- connection = ConnectWebLogin ( ) ;
286
+ newConnection = ConnectWebLogin ( ) ;
277
287
break ;
278
288
case ParameterSet_INTERACTIVE :
279
- connection = ConnectInteractive ( ) ;
289
+ newConnection = ConnectInteractive ( ) ;
280
290
break ;
281
291
case ParameterSet_ENVIRONMENTVARIABLE :
282
- connection = ConnectEnvironmentVariable ( ) ;
292
+ newConnection = ConnectEnvironmentVariable ( ) ;
283
293
break ;
284
294
}
285
295
286
296
// Ensure a connection instance has been created by now
287
- if ( connection == null )
297
+ if ( newConnection == null )
288
298
{
289
299
// No connection instance was created
290
300
throw new PSInvalidOperationException ( "Unable to connect using provided arguments" ) ;
@@ -293,9 +303,9 @@ protected void Connect(ref CancellationToken cancellationToken)
293
303
// Connection has been established
294
304
WriteVerbose ( $ "PnP PowerShell Cmdlets ({ new SemanticVersion ( Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version ) } )") ;
295
305
296
- if ( connection . Url != null )
306
+ if ( newConnection . Url != null )
297
307
{
298
- var hostUri = new Uri ( connection . Url ) ;
308
+ var hostUri = new Uri ( newConnection . Url ) ;
299
309
Environment . SetEnvironmentVariable ( "PNPPSHOST" , hostUri . Host ) ;
300
310
Environment . SetEnvironmentVariable ( "PNPPSSITE" , hostUri . LocalPath ) ;
301
311
}
@@ -309,11 +319,11 @@ protected void Connect(ref CancellationToken cancellationToken)
309
319
{
310
320
// Try requesting the site Id to validate that the site to which is being connected exists
311
321
WriteVerbose ( $ "Validating if the site at { Url } exists") ;
312
- connection . Context . Load ( connection . Context . Site , p => p . Id ) ;
322
+ newConnection . Context . Load ( newConnection . Context . Site , p => p . Id ) ;
313
323
314
324
try
315
325
{
316
- connection . Context . ExecuteQueryRetry ( ) ;
326
+ newConnection . Context . ExecuteQueryRetry ( ) ;
317
327
WriteVerbose ( $ "Site at { Url } exists") ;
318
328
}
319
329
catch ( System . Net . WebException e ) when ( e . Message . Contains ( "404" ) )
@@ -325,13 +335,13 @@ protected void Connect(ref CancellationToken cancellationToken)
325
335
326
336
if ( ReturnConnection )
327
337
{
328
- WriteObject ( connection ) ;
338
+ WriteObject ( newConnection ) ;
329
339
}
330
340
else
331
341
{
332
- PnPConnection . Current = connection ;
342
+ PnPConnection . Current = newConnection ;
333
343
}
334
- if ( CreateDrive && connection . Context != null )
344
+ if ( CreateDrive && newConnection . Context != null )
335
345
{
336
346
var provider = SessionState . Provider . GetAll ( ) . FirstOrDefault ( p => p . Name . Equals ( SPOProvider . PSProviderName , StringComparison . InvariantCultureIgnoreCase ) ) ;
337
347
if ( provider != null )
@@ -357,9 +367,9 @@ protected void Connect(ref CancellationToken cancellationToken)
357
367
private PnPConnection ConnectACSAppOnly ( )
358
368
{
359
369
CmdletMessageWriter . WriteFormattedMessage ( this , new CmdletMessageWriter . Message { Text = "Connecting with Client Secret uses legacy authentication and provides limited functionality. We can for instance not execute requests towards the Microsoft Graph, which limits cmdlets related to Microsoft Teams, Microsoft Planner, Microsoft Flow and Microsoft 365 Groups. You can hide this warning by using Connect-PnPOnline [your parameters] -WarningAction Ignore" , Formatted = true , Type = CmdletMessageWriter . MessageType . Warning } ) ;
360
- if ( PnPConnection . Current ? . ClientId == ClientId &&
361
- PnPConnection . Current ? . ClientSecret == ClientSecret &&
362
- PnPConnection . Current ? . Tenant == Realm )
370
+ if ( Connection ? . ClientId == ClientId &&
371
+ Connection ? . ClientSecret == ClientSecret &&
372
+ Connection ? . Tenant == Realm )
363
373
{
364
374
ReuseAuthenticationManager ( ) ;
365
375
}
@@ -397,14 +407,14 @@ private PnPConnection ConnectDeviceLogin()
397
407
{
398
408
Uri oldUri = null ;
399
409
400
- if ( PnPConnection . Current != null )
410
+ if ( Connection != null )
401
411
{
402
- if ( PnPConnection . Current . Url != null )
412
+ if ( Connection . Url != null )
403
413
{
404
- oldUri = new Uri ( PnPConnection . Current . Url ) ;
414
+ oldUri = new Uri ( Connection . Url ) ;
405
415
}
406
416
}
407
- if ( oldUri != null && oldUri . Host == new Uri ( Url ) . Host && PnPConnection . Current ? . ConnectionMethod == ConnectionMethod . DeviceLogin )
417
+ if ( oldUri != null && oldUri . Host == new Uri ( Url ) . Host && Connection ? . ConnectionMethod == ConnectionMethod . DeviceLogin )
408
418
{
409
419
ReuseAuthenticationManager ( ) ;
410
420
}
@@ -447,9 +457,9 @@ private PnPConnection ConnectAppOnlyWithCertificate()
447
457
throw new FileNotFoundException ( "Certificate not found" ) ;
448
458
}
449
459
X509Certificate2 certificate = CertificateHelper . GetCertificateFromPath ( CertificatePath , CertificatePassword ) ;
450
- if ( PnPConnection . Current ? . ClientId == ClientId &&
451
- PnPConnection . Current ? . Tenant == Tenant &&
452
- PnPConnection . Current ? . Certificate ? . Thumbprint == certificate . Thumbprint )
460
+ if ( Connection ? . ClientId == ClientId &&
461
+ Connection ? . Tenant == Tenant &&
462
+ Connection ? . Certificate ? . Thumbprint == certificate . Thumbprint )
453
463
{
454
464
ReuseAuthenticationManager ( ) ;
455
465
}
@@ -460,9 +470,9 @@ private PnPConnection ConnectAppOnlyWithCertificate()
460
470
var certificateBytes = Convert . FromBase64String ( CertificateBase64Encoded ) ;
461
471
var certificate = new X509Certificate2 ( certificateBytes , CertificatePassword ) ;
462
472
463
- if ( PnPConnection . Current ? . ClientId == ClientId &&
464
- PnPConnection . Current ? . Tenant == Tenant &&
465
- PnPConnection . Current ? . Certificate ? . Thumbprint == certificate . Thumbprint )
473
+ if ( Connection ? . ClientId == ClientId &&
474
+ Connection ? . Tenant == Tenant &&
475
+ Connection ? . Certificate ? . Thumbprint == certificate . Thumbprint )
466
476
{
467
477
ReuseAuthenticationManager ( ) ;
468
478
}
@@ -482,9 +492,9 @@ private PnPConnection ConnectAppOnlyWithCertificate()
482
492
{
483
493
throw new PSArgumentException ( "The certificate specified does not have a private key." , nameof ( Thumbprint ) ) ;
484
494
}
485
- if ( PnPConnection . Current ? . ClientId == ClientId &&
486
- PnPConnection . Current ? . Tenant == Tenant &&
487
- PnPConnection . Current ? . Certificate ? . Thumbprint == certificate . Thumbprint )
495
+ if ( Connection ? . ClientId == ClientId &&
496
+ Connection ? . Tenant == Tenant &&
497
+ Connection ? . Certificate ? . Thumbprint == certificate . Thumbprint )
488
498
{
489
499
ReuseAuthenticationManager ( ) ;
490
500
}
@@ -531,10 +541,10 @@ private PnPConnection ConnectCredentials(PSCredential credentials, Initializatio
531
541
ClientId = PnPConnection . PnPManagementShellClientId ;
532
542
}
533
543
534
- if ( PnPConnection . Current ? . ClientId == ClientId )
544
+ if ( Connection ? . ClientId == ClientId )
535
545
{
536
- if ( credentials != null && PnPConnection . Current ? . PSCredential ? . UserName == credentials . UserName &&
537
- PnPConnection . Current ? . PSCredential . GetNetworkCredential ( ) . Password == credentials . GetNetworkCredential ( ) . Password )
546
+ if ( credentials != null && Connection ? . PSCredential ? . UserName == credentials . UserName &&
547
+ Connection ? . PSCredential . GetNetworkCredential ( ) . Password == credentials . GetNetworkCredential ( ) . Password )
538
548
{
539
549
ReuseAuthenticationManager ( ) ;
540
550
}
@@ -574,9 +584,9 @@ private PnPConnection ConnectInteractive()
574
584
{
575
585
ClientId = PnPConnection . PnPManagementShellClientId ;
576
586
}
577
- if ( PnPConnection . Current ? . ClientId == ClientId && PnPConnection . Current ? . ConnectionMethod == ConnectionMethod . Credentials )
587
+ if ( Connection ? . ClientId == ClientId && Connection ? . ConnectionMethod == ConnectionMethod . Credentials )
578
588
{
579
- if ( IsSameOrAdminHost ( new Uri ( Url ) , new Uri ( PnPConnection . Current . Url ) ) )
589
+ if ( IsSameOrAdminHost ( new Uri ( Url ) , new Uri ( Connection . Url ) ) )
580
590
{
581
591
ReuseAuthenticationManager ( ) ;
582
592
}
@@ -618,9 +628,9 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
618
628
SecureString secPassword = StringToSecureString ( azureCertPassword ) ;
619
629
620
630
X509Certificate2 certificate = CertificateHelper . GetCertificateFromPath ( azureCertificatePath , secPassword ) ;
621
- if ( PnPConnection . Current ? . ClientId == azureClientId &&
622
- PnPConnection . Current ? . Tenant == Tenant &&
623
- PnPConnection . Current ? . Certificate ? . Thumbprint == certificate . Thumbprint )
631
+ if ( Connection ? . ClientId == azureClientId &&
632
+ Connection ? . Tenant == Tenant &&
633
+ Connection ? . Certificate ? . Thumbprint == certificate . Thumbprint )
624
634
{
625
635
ReuseAuthenticationManager ( ) ;
626
636
}
@@ -637,10 +647,10 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
637
647
SecureString secPassword = StringToSecureString ( password ) ;
638
648
var credentials = new PSCredential ( username , secPassword ) ;
639
649
640
- if ( PnPConnection . Current ? . ClientId == azureClientId )
650
+ if ( Connection ? . ClientId == azureClientId )
641
651
{
642
- if ( credentials != null && PnPConnection . Current ? . PSCredential ? . UserName == credentials . UserName &&
643
- PnPConnection . Current ? . PSCredential . GetNetworkCredential ( ) . Password == credentials . GetNetworkCredential ( ) . Password )
652
+ if ( credentials != null && Connection ? . PSCredential ? . UserName == credentials . UserName &&
653
+ Connection ? . PSCredential . GetNetworkCredential ( ) . Password == credentials . GetNetworkCredential ( ) . Password )
644
654
{
645
655
ReuseAuthenticationManager ( ) ;
646
656
}
@@ -739,7 +749,7 @@ protected override void StopProcessing()
739
749
740
750
private void ReuseAuthenticationManager ( )
741
751
{
742
- var contextSettings = PnPConnection . Current . Context ? . GetContextSettings ( ) ;
752
+ var contextSettings = Connection . Context ? . GetContextSettings ( ) ;
743
753
PnPConnection . CachedAuthenticationManager = contextSettings ? . AuthenticationManager ;
744
754
}
745
755
0 commit comments