77using System . Collections . ObjectModel ;
88using System . Globalization ;
99using System . Management . Automation ;
10+ using System . Management . Automation . Internal ;
1011using System . Management . Automation . Runspaces ;
1112using System . Reflection ;
1213using System . Text ;
@@ -19,6 +20,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
1920 /// </summary>
2021 internal static class PSObjectHelper
2122 {
23+ #region tracer
24+ [ TraceSource ( "PSObjectHelper" , "PSObjectHelper" ) ]
25+ private static readonly PSTraceSource s_tracer = PSTraceSource . GetTracer ( "PSObjectHelper" , "PSObjectHelper" ) ;
26+ #endregion tracer
27+
2228 internal const char Ellipsis = '\u2026 ' ;
2329
2430 internal static string PSObjectIsOfExactType ( Collection < string > typeNames )
@@ -290,10 +296,12 @@ internal static string SmartToString(PSObject so, PSPropertyExpressionFactory ex
290296 // take care of the case there is no base object
291297 return so . ToString ( ) ;
292298 }
293- catch ( ExtendedTypeSystemException e )
299+ catch ( Exception e ) when ( e is ExtendedTypeSystemException || e is InvalidOperationException )
294300 {
295- // NOTE: we catch all the exceptions, since we do not know
296- // what the underlying object access would throw
301+ // These exceptions are being caught and handled by returning an empty string when
302+ // the object cannot be stringified due to ETS or an instance in the collection has been modified
303+ s_tracer . TraceWarning ( $ "SmartToString method: Exception during conversion to string, emitting empty string: { e . Message } ") ;
304+
297305 if ( formatErrorObject != null )
298306 {
299307 formatErrorObject . sourceObject = so ;
0 commit comments