如何在powershell中调用函数?

How to call a function within & in powershell?

假设我在测试中有一个函数。ps1,它的名字是显示代码如下:

. "some_path\some_other_script.ps1"

function show {
    write-host "Hello World"
    #some other function call from some_other_script.ps1
    ...
}

如何以以下格式调用显示(在 & - 调用运算符中)

powershell.exe "& '%test_script_path%\test.ps1'\show"

我想我需要先进行点源测试。ps1,以便从 some_other_script.ps1

获取显示函数中的依赖项

我知道我可以创建一个新脚本并将代码放在新脚本中而不是函数中。这样,什么时候执行 &... 脚本将被调用。但我不想为一个非常简单的功能创建一个新脚本

感谢任何建议

你可以 dot source 然后这样调用它。

powershell -Command ". '%test_script_path%\toolsql.ps1'; show"

; 用作命令之间的分隔符。

非常感谢@Patrick Meinecke 的帮助和时间,以下命令有效。

powershell -command ". .\test.ps1;show"

就像他在聊天中提到的, “所以解释一下 .\ 只是表示它在当前目录中 它仍然需要另一个。告诉 powershell 将脚本加载到当前上下文中