Forum Discussion

tbresson's avatar
tbresson
Copper Contributor
Aug 13, 2021
Solved

How to re-use authentication with PnP.Powershell

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.

  • 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.
  • tbresson's avatar
    tbresson
    Copper Contributor
    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.

Resources