{"id":3121,"date":"2010-02-11T11:29:41","date_gmt":"2010-02-11T11:29:41","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2010\/02\/11\/workaround-to-catch-exceptions-from-import-clixml\/"},"modified":"2019-02-18T13:05:59","modified_gmt":"2019-02-18T20:05:59","slug":"workaround-to-catch-exceptions-from-import-clixml","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/workaround-to-catch-exceptions-from-import-clixml\/","title":{"rendered":"Workaround to Catch Exceptions from Import-CliXml"},"content":{"rendered":"<p>Recently, I came across an interesting bug in PowerShell. Let\u2019s create a repro.<\/p>\n<p>First, we create a string \u201ca\u201d and generate an xml based representation of it using the Export-Clixml cmdlet. Since we need to have the class id, we pipe the string to format-table as shown in the example below.<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #000080\">PS&gt;<\/span> <span style=\"color: #8b0000\">&quot;a&quot;<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">format-table<\/span> <span style=\"color: #000080\">-auto<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">Export-Clixml<\/span> <span style=\"color: #8a2be2\">a.xml<\/span><\/pre>\n<p><\/font><\/p>\n<p>Here is the content of the xml file.<\/p>\n<pre class=\"PowerShellColorizedScript\">&lt;Objs Version=&quot;1.1.0.1&quot; xmlns=&quot;http:\/\/schemas.microsoft.com\/powershell\/2004\/04&quot;&gt;<br \/>&#160; &lt;Obj RefId=&quot;0&quot;&gt;<br \/>&#160;&#160;&#160; &lt;TN RefId=&quot;0&quot;&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.PacketInfoData&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.FormatInfoData&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;T&gt;System.Object&lt;\/T&gt;<br \/>&#160;&#160;&#160; &lt;\/TN&gt;<br \/>&#160;&#160;&#160; &lt;ToString&gt;Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData&lt;\/ToString&gt;<br \/>&#160;&#160;&#160; &lt;Props&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;S N=&quot;ClassId2e4f51ef21dd47e99d3c952918aff9cd&quot;&gt;<font color=\"#0000ff\">27c87ef9bbda4f709f6b4002fa4af63c<\/font>&lt;\/S&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;Obj N=&quot;formatEntryInfo&quot; RefId=&quot;1&quot;&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;TN RefId=&quot;1&quot;&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.FormatEntryInfo&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;T&gt;Microsoft.PowerShell.Commands.Internal.Format.FormatInfoData&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;T&gt;System.Object&lt;\/T&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/TN&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;ToString&gt;Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry&lt;\/ToString&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;Props&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;S N=&quot;ClassId2e4f51ef21dd47e99d3c952918aff9cd&quot;&gt;29ED81BA914544d4BC430F027EE053E9&lt;\/S&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;S N=&quot;text&quot;&gt;a&lt;\/S&gt;<br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;\/Props&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;\/Obj&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;B N=&quot;outOfBand&quot;&gt;true&lt;\/B&gt;<br \/>&#160;&#160;&#160;&#160;&#160; &lt;B N=&quot;writeErrorStream&quot;&gt;false&lt;\/B&gt;<br \/>&#160;&#160;&#160; &lt;\/Props&gt;<br \/>&#160; &lt;\/Obj&gt;<br \/>&lt;\/Objs&gt;<\/font><\/pre>\n<p>Then, we remove the class id (in blue) and save the xml file. Next, we recreate an object using the import-cliXml cmdlet and we assign it to a variable, i.e. $a. We should get an exception because the class id is missing but, we do not.<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #000080\">PS&gt;<\/span> <span style=\"color: #ff4500\">$a<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Import-Clixml<\/span> <span style=\"color: #8a2be2\">.\\a.xml<\/span><\/font><\/pre>\n<p>On the other hand, if we execute the same command without assigning it to a variable, the exception is thrown.<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #000080\">PS&gt;<\/span> <span style=\"color: #0000ff\">Import-Clixml<\/span> <span style=\"color: #8a2be2\">.\\a.xml<\/span> <br \/><\/pre>\n<p><font color=\"#ff0000\" size=\"1\" face=\"Courier New\">Unknown class Id . \n    <br \/>&#160;&#160;&#160; + CategoryInfo&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : InvalidData: (Microsoft.Power&#8230;FormatEntryData:PS <\/p>\n<p>Object) [out-lineoutput], PSArgumentException <\/p>\n<p>&#160;&#160;&#160; + FullyQualifiedErrorId : FormatObjectDeserializerDeserializeInvalidClassId, <\/p>\n<p>Microsoft.PowerShell.Commands.OutLineOutputCommand<\/font><\/p>\n<h3>Why is this happening?<\/h3>\n<p>In the first instance, the object gets assigned to the variable and it never goes to the F&amp;O system. Thus the terminating error is never generated. <\/p>\n<h3>The workaround<\/h3>\n<p>The fix is fairly simple, Out-String will send the object to the host through the F&amp;O system. <\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #000080\">PS&gt;<\/span> <span style=\"color: #ff4500\">$a<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Import-Clixml<\/span> <span style=\"color: #8a2be2\">.\\a.xml<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">Out-String<\/span><\/pre>\n<p><font color=\"#ff0000\" size=\"1\" face=\"Courier New\">out-lineoutput : Unknown class Id . \n    <br \/>&#160;&#160;&#160; + CategoryInfo&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : InvalidData: (Microsoft.Power&#8230;FormatEntryData:PSObject <\/p>\n<p>&#160;&#160; ) [out-lineoutput], PSArgumentException <\/p>\n<p>&#160;&#160;&#160; + FullyQualifiedErrorId : FormatObjectDeserializerDeserializeInvalidClassId,Micros <\/p>\n<p>&#160;&#160; oft.PowerShell.Commands.OutLineOutputCommand<\/font><\/p>\n<p>To make sure our script ends nicely, we catch the terminating error. <\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">$e<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$null<\/span>\n<span style=\"color: #00008b\">try<\/span><span style=\"color: #000000\">{<\/span>            \n    <span style=\"color: #0000ff\">Import-Clixml<\/span> <span style=\"color: #8a2be2\">.\\a.xml<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">Out-String<\/span>             \n<span style=\"color: #000000\">}<\/span>            \n<span style=\"color: #00008b\">catch<\/span><span style=\"color: #000000\">{<\/span>            \n    <span style=\"color: #ff4500\">$e<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$_<\/span>            \n<span style=\"color: #000000\">}<\/span>            \n<span style=\"color: #00008b\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$e<\/span> <span style=\"color: #a9a9a9\">\u2013ne<\/span> <span style=\"color: #ff4500\">$null<\/span><span style=\"color: #000000\">)<\/span> <span style=\"color: #000000\">{<\/span> <span style=\"color: #ff4500\">$e<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">FullyQualifiedErrorId<\/span> <span style=\"color: #000000\">}<\/span><\/pre>\n<p><font size=\"1\" face=\"Courier New\">FormatObjectDeserializerDeserializeInvalidClassId,Microsoft.PowerShell.Commands.OutLineOut \n    <br \/>putCommand<\/font><\/p>\n<p>Cheers, \n  <br \/>Francisco Gomez Gamino [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I came across an interesting bug in PowerShell. Let\u2019s create a repro. First, we create a string \u201ca\u201d and generate an xml based representation of it using the Export-Clixml cmdlet. Since we need to have the class id, we pipe the string to format-table as shown in the example below. PS&gt; &quot;a&quot; | format-table [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Recently, I came across an interesting bug in PowerShell. Let\u2019s create a repro. First, we create a string \u201ca\u201d and generate an xml based representation of it using the Export-Clixml cmdlet. Since we need to have the class id, we pipe the string to format-table as shown in the example below. PS&gt; &quot;a&quot; | format-table [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}