gpt4 book ai didi

powershell 异常具有误导性 InvocationInfo

转载 作者:行者123 更新时间:2023-12-02 23:15:42 25 4
gpt4 key购买 nike

给定脚本。

$foo = @("bar")

try {
$foo | ForEach-Object {
Join-Path $null $null
}
} catch {
$_.InvocationInfo.Line
}

将打印
$foo | ForEach-Object {

但我想
Join-Path $null $null

我怎样才能得到实际引发异常的位置?

最佳答案

这将为您提供实际的线路:

$_.Exception.CommandInvocation.Line

和异常消息:
$_.Exception.Message

和行号:
$_.Exception.Line

和偏移量(列):
$_.Exception.Offset

所以你可以发出一个有用的小信息:
} catch {
$msg = "Failed to do something. Failed on line number '{0}' column '{1}' ('{2}'). The error was '{3}'." -f
$_.Exception.Line, $_.Exception.Offset, $_.Exception.CommandInvocation.Line.Trim(), $_.Exception.Message
Write-Error $msg
}

关于powershell 异常具有误导性 InvocationInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com