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
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
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
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
Many/most/all PnP SharePoint cmdlets do seem to require that the web you're working with match the one last referenced in the call to Connect-PnPOnline. Making two Connect calls and assigning each to a var does not seem to work for context switching between webs. Explicitly setting the Connection parameter in a call may still be a good idea to help trigger an error when a connection isn't available - the cmdlet does then seem to always return a null and the cmdlet will fail.
Hope this helps! -e.