What exception type should be used in Powershell to catch a XML parse error due to invalid characters?

后端 未结 2 793
一向
一向 2021-01-18 08:19

Line 2 in the script below generates -

\"Cannot convert value \"System.Object[]\" to type \"System.Xml.XmlDocument\". Error: \"\'→\', hexadecimal va

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 09:21

    Here is a way to discover yourself the full type name of an Exception, the result here gives System.Management.Automation.ArgumentTransformationMetadataException as given by @Adrian Wright.

    Clear-Host
    try {
        [xml]$xml = Get-Content "c:\Temp\1.cs" # line 2
    }
    catch {
        # Discovering the full type name of an exception
        Write-Host $_.Exception.gettype().fullName
        Write-Host $_.Exception.message
    }
    

提交回复
热议问题