中使用PowerShell的Invoke命令来调用带有参数的批处理文件 [英] Using Powershell's Invoke-Command to call a batch file with arguments
问题描述
我要为了调用远程机器上的一个批处理文件来使用PowerShell的。此批处理文件包含参数。这是我到目前为止有:
I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far:
$script = "\\fileshare\script.cmd"
$server = $args[0]
$args [string]::join(',',$args[1 .. ($args.count-1)])
Invoke-Command -computername $server {$script + ' ' + $args}
有点搜索之后,我发现,调用命令函数运行它在一个全新的过程脚本块,所以你不能把它的变量(他们不会得到扩展)。这就是-ArgumentList标签是什么。所以,我想这不是...
After a bit of searching, I found that the Invoke-Command function runs its scriptblock in a whole new process, so you can't put variables in it (they won't get expanded). That's what the -ArgumentList tag is for. So I tried this instead...
Invoke-Command -computername $server {\\fileshare\script.cmd} -ArgumentList "FirstArgument"
这是没有工作要么...我的批处理脚本告诉我这是没有传递任何参数。我找不到任何明确这么说,但它看起来像-ArgumentList参数只能在PowerShell脚本(它不会喂它们到一个批处理脚本)。
That didn't work either... my batch script tells me it's not being passed any arguments. I can't find anything that explicitly says so, but it looks like the -ArgumentList parameter only works on Powershell scripts (it won't feed them to a batch script).
任何想法我怎么能使用Invoke-Command来调用批处理文件的参数?
Any ideas how I can use Invoke-Command to call a batch file with arguments?
推荐答案
在传递参数列表的脚本块,尝试接纳他们用PARAM指令。像这样的:
When you pass the argument list to the scriptblock, try to "receive them" using a PARAM directive. Like this:
Invoke-Command -computername $server {PARAM($myArg) \\fileshare\script.cmd $myArg} -ArgumentList "FirstArgument"
或者你可以使用的$ args自动变量:
or you can just use the $args automatic variable:
Invoke-Command -computername $server {\\fileshare\script.cmd $args} -ArgumentList "FirstArgument"
这篇关于中使用PowerShell的Invoke命令来调用带有参数的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!