从命令行调用PowerShell函数 [英] Calling PowerShell function from command line
本文介绍了从命令行调用PowerShell函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的文件系统上给出以下 say-hello.ps1
文件:
function SayHello()
{
returnHello World!
}
在命令行中调用像这样任务):
powershell -ExecutionPolicy unrestricted-command& {c:\say-hello.ps1; SayHello}
为什么我会得到以下结果?
SayHello:术语'SayHello'不被识别为cmdlet,
函数,脚本文件或可操作程序的名称。检查名称的拼写,或
(如果包含路径),请验证路径是否正确,然后重试。
在行:1个字符:33
+& {c:\say-hello.ps1; SayHello}
+ ~~~~~~~~
+ CategoryInfo:ObjectNotFound:(SayHello:String)[],CommandNot
FoundException
+ FullyQualifiedErrorId:CommandNotFoundException
解决方案脚本文件的范围 c: \\ say-hello.ps1
在脚本终止时结束。你可以点源文件(注意在PS1之前的。
)如果你想让它的内容在当前范围内运行 - 用curlies {...}
:
powershell -ExecutionPolicy unrestricted-command& 。c:\say-hello.ps1; SayHello}
Given the following say-hello.ps1
file on my file system:
function SayHello()
{
return "Hello World!"
}
called on the command line like so (it will eventually run as a Windows Scheduled Task):
powershell -ExecutionPolicy unrestricted -command "& { c:\say-hello.ps1; SayHello }"
Why am I getting the following result?
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
解决方案 The scope of the script file c:\say-hello.ps1
ends when the script terminates. You can dot source the file (note the .
before the PS1) if you would like its contents to run within the current scope -- the script block enclosed with curlies {...}
:
powershell -ExecutionPolicy unrestricted -command "& { . c:\say-hello.ps1; SayHello }"
这篇关于从命令行调用PowerShell函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文