在我的文件系统上给出以下say-hello.ps1
文件:
function SayHello()
{
return "Hello World!"
}
在命令行上调用(它最终将作为Windows计划任务运行):
powershell -ExecutionPolicy unrestricted -command "& { c:\say-hello.ps1; SayHello }"
为什么我会得到以下结果?
SayHello : The term 'SayHello' 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:33
+ & { c:\say-hello.ps1; SayHello }
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (SayHello:String) [], CommandNot
FoundException
+ FullyQualifiedErrorId : CommandNotFoundException
答案 0 :(得分:6)
脚本文件c:\say-hello.ps1
的范围在脚本终止时结束。如果您希望其内容在当前范围内运行,您可以点源文件(请注意PS1之前的.
) - 用curles {...}
括起来的脚本块:
powershell -ExecutionPolicy unrestricted -command "& { . c:\say-hello.ps1; SayHello }"