powershell - 在Powershell控制台和ISE中从脚本目录中点源文件?

标签 powershell powershell-ise

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

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

但是,这会在ISE中引发错误。在控制台和ISE中都可以使用吗?我正在尝试不使用完整的绝对路径。

最佳答案

$ 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

关于powershell - 在Powershell控制台和ISE中从脚本目录中点源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8858027/

相关文章:

powershell - 如何替换特定的行号

windows - 搜索文件并将路径存储在变量中

.net - 是否可以依次运行多个powershell脚本,等待完成后再启动下一个脚本?

powershell - 列出属于特定子网的所有 azure vm

powershell - 在 ISE 中强制控制台输出?

excel - Powershell:在 Excel 中隐藏列和边界单元格

regex - Powershell foreach 路径找不到文件

powershell - 如何在powershell中解析json响应

powershell - 在数组中依次调用一组函数

excel - Powershell如何复制()工作表并将其另存为自己的工作簿