我有一个 .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 概念。我不能在脚本文件中定义函数吗?
请注意,我已经将执行策略设置为“RemoteSigned”。而且我知道在文件名前使用一个点来运行 .ps1 文件:.\myFile.ps1