Skip to content

Conversation

reusto
Copy link
Contributor

@reusto reusto commented Feb 14, 2023

Type

  • Bug Fix
  • New Feature
  • Sample

What is in this Pull Request ?

Added a connection parameter to the Connect-PnPOnline command to allow the reuse of a specific connection.

@reusto reusto changed the title Feature/provideconnection2connect Add connection paramter to Connect-PnPOnline Feb 14, 2023
@KoenZomers
Copy link
Collaborator

Interesting one. I guess the use case for this would be if you wanted to connect to a different site URL but not having to authenticate again, correct?

@KoenZomers KoenZomers self-assigned this Feb 15, 2023
@KoenZomers KoenZomers added the in review PR or issue is currently being reviewed label Feb 15, 2023
@reusto
Copy link
Contributor Author

reusto commented Feb 15, 2023

Correct, that is already the case if the current connection is stored in PnPConnection.Current, but if I save the connection to a variable by using -ReturnConnection this value is not set and can't be reused.

For me for example this is useful when using parallel threads to iterate through multiple site collections while having to use a connection method like interactive.

@KoenZomers KoenZomers force-pushed the feature/provideconnection2connect branch from 62ec334 to 6cc2dae Compare February 15, 2023 23:39
@KoenZomers
Copy link
Collaborator

Thanks @reusto !

@KoenZomers KoenZomers merged commit d2b0c24 into pnp:dev Feb 15, 2023
@MubSalim
Copy link

Hi,

The current Connect-PnPOnline documentation lists a -Connection parameter:
https://pnp.github.io/powershell/cmdlets/Connect-PnPOnline.html#-connection

but in PnP.PowerShell v1.12.0 I can't see that parameter. Is this a case of the documentation having been updated before the command is fully available or am I missing something.

image

Thanks in advance for the clarification!

@KoenZomers
Copy link
Collaborator

@MubSalim It has been added through this PR under which you are commenting. This PR was merged 12 days ago. This means it is only available in the latest nightly build, but not in 1.12. You can update to the latest nightly build using Update-Module PnP.PowerShell -AllowPrerelease

@LastGunslinger
Copy link

LastGunslinger commented Apr 13, 2023

@KoenZomers I am trying to use this new feature in PnP.PowerShell 2.1.1, and it doesn't seem to be working for me. Perhaps I'm doing something wrong or don't understand how it works? This was my simple test:

$creds = Get-Credential
$conn1 = Connect-PnPOnline 'tenant.sharepoint.com/sites/site1' -Credentials $creds -ReturnConnection
Connect-PnPOnline 'tenant.sharepoint.com/sites/site2' -Connection $conn1

My issue is, when I run the 3rd line it asks for my credentials again. It was my understanding that the "-Connection" parameter would allow creating the new connection without also needing to provide the credentials a second time, otherwise I don't see what problem this is meant to solve.

@KoenZomers
Copy link
Collaborator

@LastGunslinger, that's a bit of a quirky scenario. The change causes it now to be possible to use different authentication methods and parameters to allow for scenarions where you would want to connect using different credentials\app regs\certs\etc. However it does cause the reuse of credentials not to work this straightforward anymore as each connection is considered as a connection on its own. To mitigate this to have it do what you would like it to do, what you can do is first connect using the "normal" way, i.e.:

Connect-PnPOnline tenant.sharepoint.com/sites/site1 -Credentials $creds

Once you've done this, you should be able to create additional connections using i.e.

$conn1 = Connect-PnPOnline tenant.sharepoint.com/sites/site2 -ReturnConnection

And it should reuse the credentials from the initial connection. Give it a try and see if it works this way for you.

@LastGunslinger
Copy link

@LastGunslinger, that's a bit of a quirky scenario. The change causes it now to be possible to use different authentication methods and parameters to allow for scenarions where you would want to connect using different credentials\app regs\certs\etc. However it does cause the reuse of credentials not to work this straightforward anymore as each connection is considered as a connection on its own. To mitigate this to have it do what you would like it to do, what you can do is first connect using the "normal" way, i.e.:

Connect-PnPOnline tenant.sharepoint.com/sites/site1 -Credentials $creds

Once you've done this, you should be able to create additional connections using i.e.

$conn1 = Connect-PnPOnline tenant.sharepoint.com/sites/site2 -ReturnConnection

And it should reuse the credentials from the initial connection. Give it a try and see if it works this way for you.

@KoenZomers I tried to connect using the same method that you did above, and I am still being prompted to enter my credentials again for the second connection. This was my test:

Connect-PnPOnline tenant.sharepoint.com/sites/site1 -Credentials $adminCreds
$conn = Connect-PnPOnline tenant.sharepoint.com/sites/site2 -ReturnConnection

After the second line, it again prompted me for a username/password for the connection. Just to be sure that I was indeed using the correct PnP.PowerShell version, I ran Get-Module -Name PnP.PowerShell to make sure that the currently loaded version was 2.1.1, and it was, so I'm not sure why this isn't working for me.

@reusto
Copy link
Contributor Author

reusto commented Apr 14, 2023

@LastGunslinger I can see the reason for this behavior in the code. Therefor yes currently you need to provide the credentials additionally to the connection parameter.

@KoenZomers If its okay with you I will take a look into how to better navigate the reuse of a connection when just providing the connection.

@KoenZomers
Copy link
Collaborator

KoenZomers commented Apr 14, 2023

You always need to provide a means of authentication indeed. Not providing anything will always make it prompt for credentials.

Feel free to see how this can be improved. Was thinking about adding a flag like -reuseauthentication or something similar. Just mind to keep the option open to actually be able to use a different authentication.

@KoenZomers
Copy link
Collaborator

As the matter of fact, what is particularly interesting about this specific scenario is, you seem to be authenticating with just an username and password so without mfa. I don't see how that could be a problem in any scenario actually as you capture the credentials one using Get-credentials and then just pass them in with every connect. What am I missing here?

@LastGunslinger
Copy link

As the matter of fact, what is particularly interesting about this specific scenario is, you seem to be authenticating with just an username and password so without mfa. I don't see how that could be a problem in any scenario actually as you capture the credentials one using Get-credentials and then just pass them in with every connect. What am I missing here?

There's not really an issue here for me, which is why I didn't open this as a bug. When I do have to connect to many sites in separate threads, I just store the credentials object and re-use that, like you mentioned above. I just wasn't sure if what I was experiencing was intentional, since it didn't seem to match up with what I saw in the change log. I'm fine with continuing to re-use the credentials, I was just testing this new functionality to see if it would save any time when running my scripts. With thousands of sites, even a savings of a few seconds can make a difference :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in review PR or issue is currently being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants