From dd740d8243a65f822feefd0dd5a5d6342bdd0cc5 Mon Sep 17 00:00:00 2001 From: Giacomo Pozzoni Date: Thu, 11 Apr 2024 08:37:57 +0200 Subject: [PATCH] Fix exception in PnPConnectedCmdlet (#3885) Fix an NullDereferenceException happening when an exception is logged in PnPConnectedCmdlet but the connection passed through -Connection parameter is not the latest one. Co-authored-by: Gautam Sheth --- src/Commands/Base/PnPConnectedCmdlet.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Commands/Base/PnPConnectedCmdlet.cs b/src/Commands/Base/PnPConnectedCmdlet.cs index a4afb86c4..6888f0597 100644 --- a/src/Commands/Base/PnPConnectedCmdlet.cs +++ b/src/Commands/Base/PnPConnectedCmdlet.cs @@ -101,7 +101,10 @@ protected override void ProcessRecord() throw new PSInvalidOperationException(errorMessage); } - Connection.RestoreCachedContext(Connection.Url); + if (Connection.Context.Url != Connection.Url) + { + Connection.RestoreCachedContext(Connection.Url); + } // With ErrorAction:Ignore, the $Error variable should not be populated with the error, otherwise it should if (!ParameterSpecified("ErrorAction") || !(new[] { "ignore" }.Contains(MyInvocation.BoundParameters["ErrorAction"].ToString().ToLowerInvariant())))