2

我在目录中有以下脚本c:\scripts\

# . c:\scripts\anotherScript.ps1
function GetScriptRoot { Split-Path $script:MyInvocation.MyCommand.Path }
. "$(GetScriptroot)\anotherScript.ps1"

但是,它会在 ISE 中引发错误。这是一种在控制台和 ISE 中都适用的方式吗?我试图不使用完整的绝对路径。

4

1 回答 1

2

$MyInvocation.MyCommand.Path 属性仅在运行脚本中可用。

要检测您是否在 ISE 中运行,您可以检查$psise变量:

if ($psise) {
    "Running in the ISE"
} else {
    "Not running in the ISE"
}

或查看$host.Name属性:

PS C:\Users\andy> $host
Name             : Windows PowerShell ISE Host

PS C:\Users\andy> $host
Name             : ConsoleHost
于 2012-01-13T22:55:14.480 回答