2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

0

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\"')

Showing it working

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jamiet
  • 10,501
  • 14
  • 80
  • 159