从标准输入解释脚本时如何将参数传递给powershell命令
我正在以从标准输入解释脚本时如何将参数传递给powershell命令,powershell,powershell-1.0,Powershell,Powershell 1.0,我正在以ssh的方式通过ssh运行powershell脚本user@host“powershell-Comand-
ssh的方式通过ssh运行powershell脚本user@host“powershell-Comand-
。只要我开始传递参数,它就会正常工作
当我将其设置为powershell-Command-my args时,它会失败(如文档所示)
“-”是用-Command参数指定的;不允许使用-Command的其他参数。
另一种方法是powershell my args-Command-
它会失败,原因是:
The term 'my' 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:3
+ my <<<< args -Command -
+ CategoryInfo : ObjectNotFound: (my:String) [], CommandNotFoundE
xception
+ FullyQualifiedErrorId : CommandNotFoundException
似乎没有传递任何参数,但出于某种原因,它们会打印在控制台上。避免ssh似乎不会改变任何东西:
(cygwin) $ $SYSTEMROOT/System32/WindowsPowerShell/v1.0/powershell -Command 'Write-Host $($args.Count)' "my" "args"
0 my args
您不能直接这样做,但我认为这是可以做到的,如果您将脚本包装在scriptblock中并向其传递参数:
echo "& { $(cat script.ps1) } 'my' 'args'" | ssh user@host "powershell -Command"
由于-Command
参数无法处理多行字符串,因此有一种方法可以使用-EncodedCommand
参数的Base64编码值将其传入(尽管不是通过标准输入),但它很难看:
ssh user@host "powershell -encodedcommand $((echo "& {"; cat script.ps1 ; echo "} 'my' 'args'") | iconv -f ascii -t utf-16le | base64 -w0 ; echo -e "\n")
这一个按预期工作:
script=$(cat您是否尝试将my args
放在引号中?我尝试在两种变体中同时引用args(同时引用和单独引用)消息与未加引号的变体相同。如何使用ssh进行powershell远程处理?很好,几乎可以。当我的脚本有多行时,它似乎不起任何作用。编辑:CRLF行结尾似乎没有什么区别。@OliverGondža看起来像-Command
无法处理换行符,请参阅更新的答案。或者使用分号。
ssh user@host "powershell -encodedcommand $((echo "& {"; cat script.ps1 ; echo "} 'my' 'args'") | iconv -f ascii -t utf-16le | base64 -w0 ; echo -e "\n")
> This is 'a': THE VALUE OF THE "a"
> This is 'b': B B B B