1
如何捕獲(壓縮)PowerShell表達式中的錯誤?PowerShell管道中的錯誤
我想使用get-ChildItem遍歷目錄中的文件並提取一些信息。
get-ChildItem "c:\test\" -Recurse | % { $_.Name + " ==> " + (try{(([xml] (Get-Content $_.FullName)).DocumentElement.TagName)} catch{"not xml"}) }
我知道我可以重寫這個不使用管道,並捕捉錯誤的方式,但它可能使這一個班輪?
上面的返回「try:術語'try'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱,請檢查 的拼寫名稱,或者如果包含路徑,該路徑是正確的,然後再試一次。「