问题
I'm using PowerShell to remotely issue a command on a Linux server. I'm using plink to connect to the server.
However plink strips out double quotes in the commands I want to run. This stops those commands from working.
$user = "user"
$password = "password"
$hostname = "host"
$plinkPath = gci -Recurse | Where-Object {$_.Name -eq "plink.exe"} | select -First 1 | foreach{$_.FullName}
& $plinkPath "$User@$hostname" -pw $password "echo" "hello ab`"cdef world"
This gives the result
hello abcdef world
It lost the speech marks in between the 'b' and the 'c'.
How do I fix this?
回答1:
I hhink I've figured it, courtesy of How do I pass a literal double quote from PowerShell to a native command?. I am not entirely sure why it works, but it does:
$user = "user"
$password = "password"
$hostname = "host"
$plinkPath = gci -Recurse | Where-Object {$_.Name -eq "plink.exe"} | select -First 1 | foreach{$_.FullName}
$command = "hello ab`\`"cdef world"
& $plinkPath "$User@$hostname" -pw $password "echo" ($command -replace '(\\*)"','$1$1\"')
来源:https://stackoverflow.com/questions/34922095/plink-strips-out-double-quote-marks