Error with PNP Module and PS 5.1

Product: PowerShell Universal
Version: 3.7.10

I have a problem with the PNP Module and Powershell 5.1,
here a little test script:

whoami

$PSVersionTable.PSVersion

$SPOSite = "https://ourdomain.sharepoint.com/sites/ITTeam/"
$ListName= "User logins"

Connect-PnPOnline -Url $SPOSite -ClientId $clientId -ClientSecret $clientSecret -WarningAction Ignore

write-host "Debug: Get-PnPListItem"

$spoItem = Get-PnPListItem -List $ListName -PageSize 1

When I do run this via Powershell Universal I do get an Error:

[information] de-pscron02\svc-pscron 
[information] Major  Minor  Build  Revision 
[information] -----  -----  -----  -------- 
[information] 5      1      17763  3770 
[information] Debug: Get-PnPListItem 
[error] Exception has been thrown by the target of an invocation. 

When I run it via Powershell on the Machine directly with the same user, all is fine:


de-pscron02\svc-pscron

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  3770    
Debug: Get-PnPListItem

I noticed that running with Powershell 7 works also in PSU, but some other parts of the script do require the old AzureAD module that can only run in PS 5.1.

Help would be much appreciated.

BR
Fabian

can you see what the inner exception is for that error?

try {
$spoItem = Get-PnPListItem -List $ListName -PageSize 1
} catch { 
$_.Exception.InnerException
}

Hi Adam,
thanks for your input.

i tried your code, no output. when just showing $_ it is like this:

[information] Get-PnPListItem : Exception has been thrown by the target of an invocation. 
[information] At C:\ProgramData\UniversalAutomation\Repository\Test\Terminal.ps1:17 char:16 
[information] +     $spoItem = Get-PnPListItem -List $ListName -PageSize 1 
[information] +                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
[information]     + CategoryInfo          : InvalidOperation: (:) [Get-PnPListItem], 
[information] PSInvalidOperationException 
[information]     + FullyQualifiedErrorId : 
[information] InvalidOperation,PnP.PowerShell.Commands.Lists.GetListItem 
[information]  

i still couldnt see a reason, all looks good to me …

So I’m not sure if this is related. Here is what error I get when trying to connect to PNPOnline:

I just updated to 4.3 and have tried it with a PowerShell 7 environment and integrated. I know we have had challenges in the past with MS modules and Universal. This was working until one of the 4.x versions came out (sorry, not sure exactly which one seemed to break it).

sorry to res the thread, but i’m running into a very similar issue. using connect-pnponline with a cert thumbprint for entra app auth, and set-pnptenantsite to modify a site collector.

if i run pwsh as the service account running PSU using the exact same commands, it works perfectly.

as soon as it is trying to run the app in PSU i get the below errors. i’ve been fighting this for a bit now and i’m all out of ideas… reinstalled PnP, reinstalled the sharepoint client sdk assemblies, rebooted the box, restarted the service, etc.

if anyone can offer any advise it would be appreciated.