我有一个.ps1文件,我想在其中定义自定义函数。

假设文件名为MyFunctions。Ps1,内容如下:

Write-Host "Installing functions"
function A1
{
    Write-Host "A1 is running!"
}
Write-Host "Done"

为了运行这个脚本并理论上注册A1函数,我导航到.ps1文件所在的文件夹并运行该文件:

.\MyFunctions.ps1

这个输出:

Installing functions
Done

然而,当我尝试调用A1时,我只是得到一个错误,说明没有该名称的命令/函数:

The term 'A1' 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, verify that the path is correct and try again.
At line:1 char:3
+ A1 <<<<
    + CategoryInfo          : ObjectNotFound: (A1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我一定误解了PowerShell的一些概念。我不能在脚本文件中定义函数吗?

注意,我已经将我的执行策略设置为“remotessigned”。我知道运行。ps1文件时,在文件名前加一个点:.\myFile.ps1