$ _。异常是$ null,当在呼叫中使用–为什么?
我有一个包含以下记录函数的powershell脚本:
function LogError([string] $message, $exception = $null) {…}
在try-catch块中,当发生异常时,我称之为以下记录函数:
catch { LogError("…", $_.Exception) }
在 logerror
函数中,第二个参数始终为 $ null
。为什么?
我找不到任何文档,可以解释为什么我不能在函数调用中使用 $ _。异常
,或者我应该使用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PowerShell函数参数未使用括号 /逗号格式传递。
那是不好的
powershell将其视为一个单一的阵列参数。
这实际上与
logerror -message(“ ...”,$_。例外)
是最好的
完成工作示例
Powershell function arguments are not passed using the parenthesis / comma formatting.
That is bad
Powershell take that as a single array argument.
This is actually the same as
LogError -Message ("...",$_.Exception)
That is ok
That is best
Complete working example