获取单独变量中的错误消息详细信息

时间:2014-07-16 08:42:08

标签: powershell powershell-v2.0

我正在使用以下代码递归复制项目:

copy-item "D:\Work\HotfixHandling Testing\V03.02.01 Hotfixes"
-destination "D:\Work\HotfixHandling Testing\temp" -errorAction SilentlyContinue -errorVariable errors    

我收到一些错误,因为目标文件夹已经存在并收到错误:

Copy-Item : Item with specified name D:\Work\HotfixHandling Testing\temp\V03.02.01 Hotfixes already exists.
At D:\Work\HotfixHandling Testing\New Text Document.ps1:3 char:10
+ copy-item <<<<  "D:\Work\HotfixHandling Testing\V03.02.01 Hotfixes" -destination "D:\Work\HotfixHandling Testing\temp" -errorAction SilentlyContinue -errorVariable error
s
    + CategoryInfo          : ResourceExists: (D:\Work\HotfixH....02.01 Hotfixes:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

现在,我想从上面提到的错误文本中单独获取错误消息,脚本行号和脚本字符索引。

是否可以单独获取这些详细信息?

1 个答案:

答案 0 :(得分:1)

errors变量中捕获错误信息后,您可以通过以下属性访问所需的错误信息:

# exception message, e.g. em with specified name D:\Work\HotfixHandling Testing\temp\V03.02.01 Hotfixes already exists.
$errors.Exception.Message;

# script line number, e.g. 3
$errors.InvocationInfo.ScriptLineNumber

# character offset (index) within line, e.g. 10
$errors.InvocationInfo.OffsetInLine

如果您需要更多属性,请尝试运行$errors | Get-Member以了解其他可用属性。