2

Does anyone have an example of Get-pnpweb with connection parameter please? I'm trying to get to a different web in my script and I believe the only way to achieve this is using this parameter, but I can't seem to find an example.

Thanks Ova

3

Connection needs to be returned from PnP command when you connect to a site/web you can implement this as below.

#Web 1 already connected
Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/web1"  -Credentials $creds -ReturnConnection

#Connecting Web2 with connection
$connection = Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/web2"  -Credentials $creds -ReturnConnection

#get Web2 using connection
$webs = Get-PnPWeb -Connection $connection
| improve this answer | |
  • I need to connect 2 other webs(web2 and web3) in my script to add group of web1 to them, just thinking if it's best practice to throw so many connections around? – ova Sep 18 at 8:36
  • You can dispose the connection after done with it using Disconnect-PnPOnline -Connection $connection – Akshay Randive Sep 18 at 9:33
2

While I think this works, you might want to look into Set-PnPContext that I've added later in my answer.

$connectionA = Connect-PnPOnline -Url https://siteA.com -CurrentCredentials -ReturnConnection

$connectionB = Connect-PnPOnline -Url https://siteB.com -CurrentCredentials -ReturnConnection

Get-PnPWeb -Connection $connectionA
Get-PnPWeb -Connection $connectionB

Set-PnPContext

Description

Sets the Client Context to use by the cmdlets, which allows easy context switching. See examples for details.

Connect-PnPOnline -Url $siteAurl -Credentials $credentials
$ctx = Get-PnPContext
Get-PnPList # returns the lists from site specified with $siteAurl
Connect-PnPOnline -Url $siteBurl -Credentials $credentials
Get-PnPList # returns the lists from the site specified with $siteBurl
Set-PnPContext -Context $ctx # switch back to site A
Get-PnPList # returns the lists from site A
| improve this answer | |
  • do you know if the command closes the connection after being used as in the first example? $web2= Connect-PnPOnline -Url $url -Credentials $Cred Set-PnPGroup -Identity $owners -AddRole "Read" -connection $web2 I need to run something like this and wondering if it will close the connection automatically. – ova Sep 18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.