首页 > 解决方案 > ISE 中的 MyInvocation.MyCommand.Path 与常规 PS

问题描述

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

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

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

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

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

标签: powershellpowershell-4.0

解决方案


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

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

推荐阅读