Using Powershell's Invoke-Command to call a batch file with arguments

别说谁变了你拦得住时间么 提交于 2019-12-01 03:41:27

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"

or you can just use the $args automatic variable:

Invoke-Command -computername $server {\\fileshare\script.cmd $args} -ArgumentList "FirstArgument"

The arguments will be passed as arguments to the scriptblock and not directly to your cmd. You have to do:

Invoke-Command {param($script,$arg1) &$script $arg1 } -computername $server -ArgumentList $script,"FirstArgument"

or

Invoke-Command {&$args[0] $args[1] } -computername $server -ArgumentList $script,"FirstArgument"

PS: I don't know what you are doing with $args [string]::join(',',$args[1 .. ($args.count-1)]), it is a syntax error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!