如何将Powershell异常描述放入字符串中? [英] How can I get powershell exception descriptions into a string?

查看:59
本文介绍了如何将Powershell异常描述放入字符串中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将错误记录发送到输出流时,我希望访问Powershell打印的相同消息

I want to have access to the same message that Powershell prints when you send an error record to the output stream

示例:


这是例外消息,位于
C:\Documents和
Settings\BillBillington\Desktop\psTest\exThrower.ps1: 1
char:6
+掷<<<< (新对象ArgumentException(这是
异常));
+ CategoryInfo:OperationStopped:(:) [],
ArgumentException
+ FullyQualifiedErrorId:这是例外情况

This is the exception message At C:\Documents and Settings\BillBillington\Desktop\psTest\exThrower.ps1:1 char:6 + throw <<<< (New-Object ArgumentException("This is the exception")); + CategoryInfo : OperationStopped: (:) [], ArgumentException + FullyQualifiedErrorId : This is the exception

当我通过执行$ Error [0]获得最后的ErrorRecord时,我似乎无法弄清楚如何以简单的方式获取此信息

I when a get the last ErrorRecord by doing $Error[0] I can't seem to figure out how to get this information in a simple way

我从社区扩展这里可以大致执行我想要的操作,但是它会打印大量不需要的东西的半格式列表,然后我就必须剥离

I found this 'Resolve-Error' function from the community extensions here which does roughly what I want but it prints a huge semi-formatted list of stuff I don't need that I have to then strip

是否可以访问Powershell使用的消息,或者无法通过一种更简单的方式来获取我关心的值的散列,从而可以将它们放入我选择的格式的字符串中?

Is there way of accessing the message that Powershell uses or failing that a simpler way of getting hash of the values I care about so I can put them into a string in a format of my choosing?

推荐答案

如果您想要一条比@tomasr短的消息(有时对用户更友好?),建议这样做:

If you want a bit shorter message (more user friendly sometimes?) than @tomasr suggests this will do:

$error[0].ToString() + $error[0].InvocationInfo.PositionMessage

您将得到类似的内容:

Cannot find path 'C:\TEMP\_100804_135716\missing' because it does not exist.
At C:\TEMP\_100804_135716\test.ps1:5 char:15
+   Get-ChildItem <<<<  missing

此技术信息将被排除:

+ CategoryInfo          : ObjectNotFound: (C:\TEMP\_100804_135716\missing:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

这篇关于如何将Powershell异常描述放入字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆