从命令行调用PowerShell函数
给定我的文件系统上的以下从命令行调用PowerShell函数,powershell,cmd,Powershell,Cmd,给定我的文件系统上的以下say hello.ps1文件: function SayHello() { return "Hello World!" } 像这样在命令行上调用(它最终将作为Windows计划任务运行): 为什么我会得到以下结果 SayHello : The term 'SayHello' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t
say hello.ps1
文件:
function SayHello()
{
return "Hello World!"
}
像这样在命令行上调用(它最终将作为Windows计划任务运行):
为什么我会得到以下结果
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
脚本文件
c:\say hello.ps1
的作用域在脚本终止时结束。如果希望文件内容在当前范围内运行,可以点源文件(注意PS1之前的
),即用卷曲括起来的脚本块{…}
:
powershell -ExecutionPolicy unrestricted -command "& { . c:\say-hello.ps1; SayHello }"
我有一个从powershell调用的函数,如下所示:展开ZIPFile–文件“E:\MYDOC.zip”-目标“E:\New MYDOC”powershell可以执行此操作,但在使用上述方法从命令行执行此操作时,会出现错误“一元运算符后缺少表达式”。有什么建议吗?您似乎都“修饰过了”“示例中的破折号和引号:
–
,”
,”
。我将使用ASCII-
和“
再次尝试使用cmd
。我有一个函数:函数Expand-ZIPFile($file,$destination){$shell=new-object-com-shell.application$zip=$shell.NameSpace($file)foreach($zip.items()){$shell.NameSpace($destination.copyhere($item)}
powershell -ExecutionPolicy unrestricted -command "& { . c:\say-hello.ps1; SayHello }"