[BUG]Get-PnPContext/Set-PnPContext not working as documented; cannot find context.
Reporting an Issue or Missing Feature
Please confirm what it is that *you're reporting Get-PnPContext not working as documented. - Throws an error.
Expected behavior
Please describe what output you expect to see from the PnP PowerShell Cmdlets Expected return of PnP Connection Context.
Actual behavior
Please describe what you see instead. Please provide samples of output or screenshots. Error: Get-PnPcontext : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
Steps to reproduce behavior
$Conn = Connect-PnPOnline -URL $SiteURL -Interactive -ReturnConnection (am prompted for User creds, MFA functions as expected)
$Ctx = Get-PnPcontext Get-PnPcontext : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect. At line:1 char:12
-
$Ctx = Get-PnPcontext -
~~~~~~~~~~~~~~- CategoryInfo : NotSpecified: (:) [Get-PnPContext], InvalidOperationException
- FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetSPOContext
$Conn ConnectionType : O365 InitializationType : Unknown Scopes : PSCredential : ClientId : <valid client ID> ClientSecret : ApplicationInsights : PnP.PowerShell.ALC.ApplicationInsights Url : <Valid & Correct URL as supplied in variable $SiteURL> TenantAdminUrl : Certificate : DeleteCertificateFromCacheOnDisconnect : False Context : PnP.Framework.PnPClientContext Tenant : AzureEnvironment : Production
What is the version of the Cmdlet module you are running?
1.11.0 PnP.PowerShell
Which operating system/environment are you running PnP PowerShell on?
- [x] Windows 10 Powershell ISE
..Rolling back to PnP.PowerShell 1.10.0 stops Get-PnPContext from throwing the error.
Same issue here with 1.11. Rolling back as suggested helped but can't be the final solution.
If you use -ReturnConnection you need to apply -connection switch on all consecutive commands.
I do, but the error occurs anyway.
Just tested it and it works, you might want to close all ISE/PowerShell windows first, and make sure you are on 1.11.0
$Conn = Connect-PnPOnline -URL $SiteURL -Interactive -ReturnConnection $Ctx = Get-PnPcontext -connection $Conn
If you use -ReturnConnection you need to apply -connection switch on all consecutive commands. Just tested it and it works, you might want to close all ISE/PowerShell windows first, and make sure you are on 1.11.0 $Conn = Connect-PnPOnline -URL $SiteURL -Interactive -ReturnConnection $Ctx = Get-PnPcontext -connection $Conn
Ugh. really? That then becomes a total hassle. it defeats the purpose of having the context. I'm supposed to rewrite ALL my custom functions just to pass them a connection variable? that's ludicrous.
function NiftyUserFunction () {
Write-Host "I'm doing fun stuff"
Get-PnPFolder -url '/' # folder object
}
$Conn = Connect-PnPOnline -URL $SiteURL -Interactive -ReturnConnection
$Ctx = Get-PnPcontext -connection $Conn
$Conn2 = Connect-PnPOnline -URL $SecondSiteURL -Interactive -ReturnConnection
$Ctx2 = Get-PnPcontext -connection $Conn2
NiftyUserFunction
Set-pnpcontext $Ctx -connection $Conn
NiftyUserFunction
Set-pnpcontext $Ctx2 -connection $Conn2
NiftyUserFunction
RETURNS:
I'm doing fun stuff
Get-PnPFolder : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
At line:6 char:6
+ Get-PnPFolder -url '/' # folder object
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-PnPFolder], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Files.GetFolder
I'm doing fun stuff
Get-PnPFolder : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
At line:6 char:6
+ Get-PnPFolder -url '/' # folder object
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-PnPFolder], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Files.GetFolder
I'm doing fun stuff
Get-PnPFolder : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
At line:6 char:6
+ Get-PnPFolder -url '/' # folder object
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-PnPFolder], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Files.GetFolder
I agree, it is a major change, I would have expected at least a warning on the connect-pnponline help page. You can rollback to the old module and update over time, I spent most of the week doing the same.
I agree, it is a major change, ... You can rollback to the old module and update over time, I spent most of the week doing the same.
/re-reads comment:
If you use -ReturnConnection you need to apply -connection switch on all consecutive commands.
🤔 "If?" So I went the other way. It seems to behave itself if you omit the -ReturnConnection switch altogether:
function NiftyUserFunction () {
Write-Host "I'm doing fun stuff" -f Yellow
Get-PnPFolder -url '/' # folder object
}
$Conn = Connect-PnPOnline -URL $SiteURL -Interactive
$Ctx = Get-PnPcontext
$Conn2 = Connect-PnPOnline -URL $SecondSiteURL -Interactive
$Ctx2 = Get-PnPcontext
NiftyUserFunction
Set-pnpcontext $Ctx
NiftyUserFunction
Set-pnpcontext $Ctx2
NiftyUserFunction
Set-pnpcontext $Ctx
NiftyUserFunction
Set-pnpcontext $Ctx2
NiftyUserFunction
RETURNS:
I'm doing fun stuff
I'm doing fun stuff
Name Type Items/Size Last Modified
---- ---- ---------- -------------
filesharing Subweb 0 4/09/2020 1:26:32 AM
Subweb 0 2/09/2020 5:15:38 AM
I'm doing fun stuff
filesharing Subweb 0 4/09/2020 1:26:32 AM
I'm doing fun stuff
Subweb 0 2/09/2020 5:15:38 AM
I'm doing fun stuff
filesharing Subweb 0 4/09/2020 1:26:32 AM
Not sure what's happening with the first call not returning anything and the second returning twice... but I can figure that out and manage it later. Perhaps some elaboration on how and why to use the -ReturnConnection switch is required.
Oh this is infuriating. If you DON'T stipulate -ReturnConnection, you run into this problem: https://github.com/pnp/powershell/issues/1729 which.. despite comments to the contrary, hasn't been fixed and still exists in 1.11.0.
For a bit of context around the change, you might want to read up on the discussion that has gone on here: https://github.com/pnp/powershell/issues/2071
I would need to test if there's an issue with specifically the Set-PnPContext cmdlet. Having said that, I don't really follow your intended use of PnP PowerShell. The idea is that you can either work with one connection and one context, in which case you omit the -ReturnConnection parameter. If you do wish to work with multiple connections at the same time, you provide the -ReturnConnection and you specify -Connection with ALL your cmdlets to make clear which connection you wish to run the cmdlet on, how else would it know? With this in mind, I personally would not know when you would ever use Get-PnPContext or Set-PnPContext. I don't use them at least. If you set up the connection properly from the start, you should be good to go without them.
Thanks for the response..
For a bit of context around the change, you might want to read up on the discussion that has gone on here: #2071
Will do.
... I don't really follow your intended use of PnP PowerShell. The idea is that you can either work with one connection and one context, ..
Consider the scenario that we have Two sites, one for internal collaboration on projects (we'll call this 'Projects'), the other for sharing documents with external parties (clients, contractors, etc). (We'll call this 'Shared') For security, External parties don't have access to the Project site. At all. Different contractors may have different access rights to different subfolders of their Project folder on the Shared Site. When a Project is done, it may need to be archived. (ie, removed from sharepoint). When this happens, all files pertaining to the project from both sites get archived (to other cloud cold-storage), and external parties no longer have access. Ergo, during the archiving/packaging up process, it's necessary to switch between contexts of Project and Shared site to compare files, versions, etc as Powershell "collates" the project data into one archive.
Thanks for elaborating @Cadder. I understand your scenario where you want to work with two sites at the same time. I would personally feel switching the current context between them increases the risk of accidently doing something when the wrong context is active. Therefore, in my opinion, it works most intuitive and lesser chance of errors if you are working with multiple connections, to make use of the -ReturnConnection and to do provide the proper connection you wish to use with each cmdlet by adding -Connection. This is what has been made possible in 1.11 and wasn't possible over the full spectrum of PnP PowerShell before.
Technically, switching contexts on a single connections should remain working, so if you still prefer using that instead, we'll have to look into why this would not work anymore. It is a scenario that I personally haven't tested and nobody reported problems with in the nightly builds of 1.10. I'll update this thread once I've had a chance to dive into it.
Thanks @KoenZomers. The same archiving script is also now throwing this error, as (kinda) reported in https://github.com/pnp/powershell/issues/2099 :
Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Recycle -Force
Remove-PnPFile : Value does not fall within the expected range.
At C:\NBRS\OneDrive - NBRS\Scripts\_Sharepoint_Maint\NBRS_SPmaint_lib.v1.02.ps1:188 char:17
+ ... try{Remove-PnPFile -Connection $Con...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Remove-PnPFile], ServerException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.RemoveFile
...where $pnpFileObj is a valid, but unrequired file.
The error is a warning level, as the script proceeds, but it still doesn't delete the file as required. This was working in 1.10.0 (and still does). Is it related to the changes you mentioned?
I've tries it with and without -Force, with and without -Recycle . makes no difference.
Try to send the output of the command to variable, I had similar issue for the add counterparts.
$remove1=Remove-PnPFile -Connection $Conn -ServerRelativeURL $pnpFileObj.ServerRelativeURL -Recycle -Force
@dimitargeorgievmitev Sending Remove-PNPFile to a variable made no difference. I still got the error. However I did some testing and figured I could replace the Remove-PNPFile with Remove-PNPListItem.
The following might be related:
Back in 1.10 I was able to change context to another site by doing so:
$siteConn1 = Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/someSite1" -Interactive -ReturnConnection
$web1 = Get-PnPWeb -Connection $siteConn1
# do other stuff in Site 1
$siteConn2 = Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/someSite2" -Interactive -ReturnConnection
$web2 = Get-PnPWeb -Connection $siteConn2
# do other stuff in Site 2
This did not open an interactive login prompt for the second connection using PnP.PowerShell 1.10.
But since 1.11 I am prompted for a login on each connection and I have to choose which account I want to use to authenticate. This makes maintenance tasks iterating over multiple sites a pain.