无法绑定参数到参数 ‘Exception’,因为它为空。

huangapple go评论65阅读模式
英文:

Write-Error: Cannot bind argument to parameter 'Exception' because it is null

问题

I’ve run into a strange error using WinSCP's .NET Assembly to develop an SFTP client. It occurs when I call PutFileToDirectory() as follows:

try {
   $hostDirectory = Get-PathBelowWebRoot -root $pcRoot -file $file
   $remove = $false
   $session.PutFileToDirectory($file.fullName, $hostDirectory, $remove, $options)
}
catch {
   $errorObject = $Error[0]
   Write-Error $errorObject
   $($errorObject.InvocationInfo)
   exit
}

Upon calling PutFileToDirectory, execution goes to the catch block, and PowerShell issues this message:

Write-Error: Cannot bind argument to parameter 'Exception' because it is null.

I've used $Error[0] successfully in the past to get error details, but for some reason it doesn’t work here.

Does anyone know how to discover the error details?

Output from $PSVersionTable.PSVersion:

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      3      4

I’m using the PowerShell extension to Visual Studio Code, version 1.79.0

英文:

I’ve run into a strange error using WinSCP's .NET Assembly to develop an SFTP client. It occurs when I call PutFileToDirectory() as follows:

try {
   $hostDirectory = Get-PathBelowWebRoot -root $pcRoot -file $file
   $remove = $false
   $session.PutFileToDirectory($file.fullName, $hostDirectory, $remove, $options)
}
catch {
   $errorObject = $Error[0]
   Write-Error $errorObject
   $($errorObject.InvocationInfo)
   exit
}

Upon calling PutFileToDirectory, execution goes to the catch block, and PowerShell issues this message:

> Write-Error: Cannot bind argument to parameter 'Exception' because it is null.

I've used $Error[0] successfully in the past to get error details, but for some reason it doesn’t work here.

Does anyone know how to discover the error details?

Output from $PSVersionTable.PSVersion:

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      3      4

I’m using the PowerShell extension to Visual Studio Code, version 1.79.0

答案1

得分: 1

这个答案并没有解释你的症状,但基于你的反馈,提供了一个有效的解决方法:

  • 在你的 catch 块中,不需要使用 自动变量 $Error,而是使用 自动变量 $_ 来引用当前的错误。

  • 如果仅仅打印错误的 消息,即其 字符串表示 足够的话,你可以使用:

    Write-Error -Message $_
    

另外:如果在脚本中以响应 错误 使用 exit,最好传递一个 非零的退出代码 给调用者,以表示发生了错误;例如 exit 1

英文:

This answer doesn't explain your symptom, but - based on your feedback - offers an effective workaround:

  • There's no need to use the automatic $Error variable in your catch block - instead, use the automatic $_ variable to refer to the error at hand.

  • If merely printing the error's message, i.e. its string representation is enough, you can use:

    Write-Error -Message $_
    

As an aside: if you're using exit in a script in response to an error, it's best to pass it a nonzero exit code to signal to the caller that an error occurred; e.g. exit 1.

huangapple
  • 本文由 发表于 2023年6月13日 04:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定