我有一个.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