Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Text;
Expand All @@ -19,6 +20,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// </summary>
internal static class PSObjectHelper
{
#region tracer
[TraceSource("PSObjectHelper", "PSObjectHelper")]
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("PSObjectHelper", "PSObjectHelper");
#endregion tracer

internal const char Ellipsis = '\u2026';

internal static string PSObjectIsOfExactType(Collection<string> typeNames)
Expand Down Expand Up @@ -290,10 +296,12 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex
// take care of the case there is no base object
return so.ToString();
}
catch (ExtendedTypeSystemException e)
catch (Exception e) when (e is ExtendedTypeSystemException || e is InvalidOperationException)
{
// NOTE: we catch all the exceptions, since we do not know
// what the underlying object access would throw
// These exceptions are being caught and handled by returning an empty string when
// the object cannot be stringified due to ETS or an instance in the collection has been modified
s_tracer.TraceWarning($"SmartToString method: Exception during conversion to string, emitting empty string: {e.Message}");

if (formatErrorObject != null)
{
formatErrorObject.sourceObject = so;
Expand Down