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 …