0

we have a powershell script wich suddenly stopped working.

The first get method after logging in:

Connect-PnPOnline -Url $url -ClientId $AppId -ClientSecret $AppSecret -verbose

started to throw error:

PS C:\Users\Balint.Rozsa> C:\Temp\Test.ps1
VERBOSE: PnP PowerShell Cmdlets (1.7.0.0)
Get-PnPListItem : Exception has been thrown by the target of an invocation.
At C:\Temp\Test.ps1:20 char:10
+ $items = Get-PnPListItem -List "Settings"
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-PnPListItem], TargetInvocationException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem

It seems like the Appid and Appsecret are invalid, but it is not true, because the script runs without any errors on other machines with the same connection parameters and same version on PnP.Powershell.

Another interesting thing is that logging in with flag -Interactive is working well, but it is not usable in scheduled scripts.

Any ideas how to get the script working on the original machine (behind firewall)?

Get-PnpException returns:

Message          : Exception has been thrown by the target of an invocation.
Stacktrace       :    at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
                      at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
                      at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                      at Microsoft.SharePoint.Client.ClientContextExtensions.<>c__DisplayClass10_0.<Clone>b__0(Object oSender, WebRequestEventArgs webRequestEventArgs)
                      at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
                      at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
                      at Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor()
                      at Microsoft.SharePoint.Client.ClientContext.<GetFormDigestInfoPrivateAsync>d__37.MoveNext()
                   --- End of stack trace from previous location where exception was thrown ---
                      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      at Microsoft.SharePoint.Client.ClientContext.<EnsureFormDigestAsync>d__36.MoveNext()
                   --- End of stack trace from previous location where exception was thrown ---
                      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      at Microsoft.SharePoint.Client.ClientContext.<ExecuteQueryAsync>d__28.MoveNext()
                   --- End of stack trace from previous location where exception was thrown ---
                      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      at Microsoft.SharePoint.Client.ClientContextExtensions.<ExecuteQueryImplementation>d__6.MoveNext()
                   --- End of stack trace from previous location where exception was thrown ---
                      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                      at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry(ClientRuntimeContext clientContext, Int32 retryCount, String userAgent)
                      at PnP.PowerShell.Commands.PnPWebRetrievalsCmdlet`1.GetWeb()
                      at PnP.PowerShell.Commands.Lists.GetList.ExecuteCmdlet()
                      at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord()
ScriptLineNumber : 26
2
  • try this: wrap the code in a try/catch, catch the typed exception and look for the InnerException property. I think the exception you received is a wrapper. Commented Mar 9, 2023 at 18:11
  • try catch does not give more info, but i have added the result of Get-PnPException
    – Bálint
    Commented Mar 10, 2023 at 10:19

2 Answers 2

1

On new tenants, ACS app-only is disabled by default. You may want to check that it's enabled if you're using a new tenant.

I would consider using Azure AD App-Only permissions to access SharePoint Online instead of ACS app-only. Microsoft recommend using the Azure AD app-only model because it's modern and more secure.

1
  • Thanks for advice, we will try it. But actually the script is working on other machines. It must be something with the firewall or proxy settings.
    – Bálint
    Commented Mar 14, 2023 at 14:54
1

The problem was with the proxy.

We got the evidence with testing script:

Connect-PnPOnline -Url $url -ClientId $AppId -ClientSecret $AppSecret -verbose
$ctx = Get-PnPContext
$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()

wich returned the whole error page in html, where we found the error message from the proxy server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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