SOLVED

How to re-use authentication with PnP.Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-2645485%22%20slang%3D%22en-US%22%3EHow%20to%20re-use%20authentication%20with%20PnP.Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2645485%22%20slang%3D%22en-US%22%3E%3CP%3EHello.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20make%20a%20Powershell%20script%20which%20gathers%20all%20of%20our%20Microsoft%20Groups%20and%20then%20go%20through%20each%20of%20them%20searching%20for%20specific%20file-%20%26amp%3B%20folder%20content.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20SharePointPnPPowerShellOnline%20I%20could%20pass%20in%20a%20Credential%20object%20and%20re-use%20that%20to%20first%20connect%20to%20the%20tenant%20and%20then%20later%20to%20each%20site%20using%20the%20-Credential%20parameter.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20PnP.Powershell%20(the%20new%20crossplatform%20version)%20I%20can't%20figure%20out%20how%20to%20re-use%20my%20authentication.%3C%2FP%3E%3CP%3EFirst%20I%20connect%20to%20the%20tenant%20and%20then%20I%20have%20to%20connect%20again%20to%20each%20site%20(Microsoft%20Group).%20By%20doing%20this%20I%20get%20more%20than%20one%20credential%20request.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20use%20the%20recommended%20way%20of%20connecting%3A%26nbsp%3B%3CSPAN%3EConnect-PnPOnline%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Url%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%24Url%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Interactive%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EGet%20the%20sites%20with%3A%26nbsp%3B%3C%2FSPAN%3EGet-PnPTenantSite%3C%2FP%3E%3CP%3EAnd%20then%20for%20each%20site%20I'm%20trying%3A%26nbsp%3B%3CSPAN%3EConnect-PnPOnline%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Url%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%24Site%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B-Interactive%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EIs%20there%20someway%20I%20can%20re-use%20my%20authentication%20%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ENB!%20I'm%20using%20MFA%20on%20my%20account.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2645485%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2652100%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20re-use%20authentication%20with%20PnP.Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2652100%22%20slang%3D%22en-US%22%3EThey%20way%20I%20found%20to%20solve%20it%20was%20this%20-%20if%20anyone%20is%20looking%20for%20the%20same%20thing%3A%3CBR%20%2F%3EUse%20the%20-ReturnConnection%20parameter%20to%20return%20the%20current%20connection%20to%20a%20variable.%3CBR%20%2F%3E%3CBR%20%2F%3E%24Connection%20%3D%20Connect-PnPOnline%20-Url%20%24Url%20-Interactive%20-ReturnConnection%3CBR%20%2F%3E%3CBR%20%2F%3ELater%20when%20I%20connect%20to%20the%20different%20sites%20I%20use%20the%20ClientId%20which%20is%20returned%20to%20the%20Connection%20object%20like%20this%3A%3CBR%20%2F%3E%3CBR%20%2F%3EConnect-PnPOnline%20-Url%20%24Site%20-Interactive%20-ClientId%20%24Connection.ClientId%3CBR%20%2F%3E%3CBR%20%2F%3EI%20still%20get%20authentication%20popups%20for%20sites%20that%20won't%20allow%20me%20to%20connect%20though%20but%20I%20guess%20this%20is%20fine.%3C%2FLINGO-BODY%3E
New Contributor

Hello.

 

I'm trying to make a Powershell script which gathers all of our Microsoft Groups and then go through each of them searching for specific file- & folder content.

 

With SharePointPnPPowerShellOnline I could pass in a Credential object and re-use that to first connect to the tenant and then later to each site using the -Credential parameter.

 

With PnP.Powershell (the new crossplatform version) I can't figure out how to re-use my authentication.

First I connect to the tenant and then I have to connect again to each site (Microsoft Group). By doing this I get more than one credential request.

 

I use the recommended way of connecting: Connect-PnPOnline -Url $Url -Interactive

Get the sites with: Get-PnPTenantSite

And then for each site I'm trying: Connect-PnPOnline -Url $Site -Interactive

 

Is there someway I can re-use my authentication ?

 

NB! I'm using MFA on my account.

1 Reply
best response confirmed by tbresson (New Contributor)
Solution
They way I found to solve it was this - if anyone is looking for the same thing:
Use the -ReturnConnection parameter to return the current connection to a variable.

$Connection = Connect-PnPOnline -Url $Url -Interactive -ReturnConnection

Later when I connect to the different sites I use the ClientId which is returned to the Connection object like this:

Connect-PnPOnline -Url $Site -Interactive -ClientId $Connection.ClientId

I still get authentication popups for sites that won't allow me to connect though but I guess this is fine.