【问题标题】:MyInvocation.MyCommand.Path in ISE vs regular PSISE 中的 MyInvocation.MyCommand.Path 与常规 PS
【发布时间】:2019-05-30 14:24:21
【问题描述】:

我有一个旧脚本,它在特定情况下使用一些额外的参数调用自己。如果通过常规的 powershell 窗口运行,这将有效

$spath = $script:MyInvocation.MyCommand.Path
$FilePathWithQuotes = '"{0}"' -f $spath 
powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs

但如果它通过 ISE 运行,我会收到此错误。

powershell : Add-Type : 无法将参数“路径”绑定到目标。 异常设置“路径”:“在 D:\Deploy\File 找不到路径 部署.ps1:39 字符:5 + powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ + CategoryInfo : NotSpecified: (Add-Type : Cann...nnot find path :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

我添加了第二行以确保空格不是问题,但它在 ISE 中仍然失败。 想法?我在 ps 5.1 上

【问题讨论】:

  • 查看控制台/ise/vscode 的答案:stackoverflow.com/questions/48511874/…
  • $script:MyInvocation.MyCommand.Path 也应该在 ISE 中工作(假设选项卡的内容已保存到脚本文件中)。顺便说一句,没有必要将文件路径包含在嵌入的双引号中(我很惊讶它甚至可以工作)。请注意错误与 Add-Type 的关系,因此您需要显示该调用以进行进一步诊断。

标签: powershell powershell-4.0


【解决方案1】:

是的,检查$PSISE,并尽可能使用它。

$spath = If($PSISE){$psISE.CurrentFile.FullPath}else{$script:MyInvocation.MyCommand.Path}
$FilePathWithQuotes = '"{0}"' -f $spath 
powershell -file $FilePathWithQuotes -NestedCall @ExtraArgs

【讨论】:

  • $script:MyInvocation.MyCommand.Path 也应该在 ISE 中工作(假设选项卡的内容已保存到脚本文件),因此不需要 if 语句(至少在 PSv5.1 中)。顺便说一句,值得指出的是,没有必要将文件路径包含在嵌入的双引号中(我很惊讶它甚至可以工作)
猜你喜欢
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2012-10-15
相关资源
最近更新 更多