Microsoft 365: PnP PowerShell – Resolving Error – “Exception has been thrown by the target of invocation” – A Small Lesson Learning for PnP PowerShell CMDLETs

PnP PowerShell - resolving error - "Exception has been thrown by the target of invocation"
PnP PowerShell - resolving error - "Exception has been thrown by the target of invocation"

Hi All,

Greetings for the day.

Today sharing small lesson learning related to error while executing then PnP PowerShell CMDLETs.

Background

  • We were executing PnP PowerShell CMDLET – Get-PnPSite.
  • Our purpose is to get the certain properties of the given site.
  • While executing this CMDLET we were getting an error.

Issue / Error

Get-PnPSite : Exception has been thrown by the target of an invocation.
At line:1 char:1

  • Get-PnPSite
  • ~~~
    • CategoryInfo : InvalidOperation: (:) [Get-PnPSite], PSInvalidOperationException
    • FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Site.GetSite
nP PowerShell - Error while executing the CMDLET - Get-PnPSite - Exception has been thrown by the target of an invocation.
fig : PnP PowerShell – Error while executing the CMDLET – Get-PnPSite. An exception occurred during invocation.

Cause

  • I am connecting the site using wrong ClientSecret.
  • Executing the Connect-PnPOnline CMDLET with wrong credentials. ClientSecret key is the wrong.
  • But, Connect-PnPOnline not throwing an exception, see the image below.
PnP PowerShell - executing the CMDLET Connect-PnPOnline
fig : PnP PowerShell – executing the CMDLET Connect-PnPOnline – with wrong credentials – ClientSecret is wrong here.

LESSON LEARNED

  • This error applies to all PnP PowerShell CMDLETs which are executed after Connect-PnPOnline with wrong credentials.
  • For any PnP PowerShell if such error “Exception has been thrown by the target of an invocation.” occurs, verify the credentials first.

Thanks for reading 🙂 ENJOY BEAUTIFUL LIFE 🙂 SHARING IS CARING 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.