如何使公共功能

use*_*307 2 import powershell function

我想在文件functions.ps1中定义函数,然后从另一个脚本调用它.像这样的东西:

Functions.ps1:

Function Hi()
{
"hi"
}
Run Code Online (Sandbox Code Playgroud)

从另一个脚本(Call.ps1)调用它.

Call.ps1:

invoke-expression -Command .\functions.ps1
Hi 
Run Code Online (Sandbox Code Playgroud)

但是函数是在脚本函数的本地范围内定义的.我得到的错误:

The term 'hi' is not recognized as the name of a cmdlet, function,
script file , or operable program. Check the spelling of the name, or
if a path was included, v erify that the path is correct and try
again.
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法来解决这个问题?

Mar*_*ndl 6

您必须对脚本进行dotource以将其加载到当前的运行空间中:

. .\functions.ps1
Hi
Run Code Online (Sandbox Code Playgroud)