我在ssh上运行powershell脚本为"jobs": { // Here i need [ instead of {, how can i fix it?
"2": {
"working": "1"
},
"5": {
"working": "4"
},
"6": {
"working": "3"
}
}
$return = array("jobs" => $return,
"testing" => 'testing');
。只要我开始传递参数,它就会按预期工作。
当我将其作为ssh user@host "powershell -Comand - < script.ps1
时,它会失败(如文档所述)powershell -Command - my args
虽然
'-' was specified with the -Command parameter; no other arguments to -Command are permitted.
的另一种方式失败了:
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似乎没有任何改变:
(local bash) $ echo '\n' | ssh -i master-key Admin@10.8.55.78 '$SYSTEMROOT/System32/WindowsPowerShell/v1.0/powershell' -Command 'Write-Host \$\(\$args.Count\)' "my" "args"
0 my args
答案 0 :(得分:1)
你不能直接这样做,但我认为如果你将脚本包装在scriptblock中并将参数传递给它,我可以这样做:
echo "& { $(cat script.ps1) } 'my' 'args'" | ssh user@host "powershell -Command"
由于-Command
参数无法处理多行字符串,因此有一种方法可以使用Base64编码的-EncodedCommand
参数值将其传入(虽然不是通过标准输入),但它很难看:
ssh user@host "powershell -encodedcommand $((echo "& {"; cat script.ps1 ; echo "} 'my' 'args'") | iconv -f ascii -t utf-16le | base64 -w0 ; echo -e "\n")
答案 1 :(得分:0)
这一工作正常:
script=$(cat <<-'SCRIPT'
{
$a=$Args[0];
$b=$Args[1];
# Do not enclose $script into "" to avoid this comment spread till the EOL
Write-Host "This is 'a': $a";
Write-Host "This is 'b': $b";
} # <- call as [[[ -c "& { $script } ... " ]]] if you ommit braces '{}' here
SCRIPT
)
a="THE VALUE OF THE \"a\""
b="B B B B"
powershell -nologo -executionpolicy bypass -c "& $script '$a' '$b'"
输出:
> This is 'a': THE VALUE OF THE "a"
> This is 'b': B B B B