Kroups
Kroups

Reputation: 13

How use character '$' in a Invoke-SSHCommand without error

I try to get information from a Linux server in a powershell script

I use the Windows forms and i get the informations in a RichTextBox.

There is no problem for some basic commands.

Example :

$infoversion = Invoke-SSHCommand -Index 0 -Command "uname -r"

$Result_Info.text += "Kernel Version : $($infoversion.Output) + ("`n")

But when i use a command with a pipe and the '$' character, it doesn't work.

Example :

$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print $2,$3,$4,$5,$6,$7}'"

$Result_Info.text += "Server up since $($infouptime.Output)" + ("`n")

(This command work directly on the server)

I try to set \$ or 'e or '$' or "+chr(38)+" but nothing works

If someone can help me, it will be nice :)

Upvotes: 1

Views: 700

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

PowerShell uses the backtick ` as the escape character in expandable strings:

$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print `$2,`$3,`$4,`$5,`$6,`$7}'"

Alternatively, since you don't actually have any variables that need expanding, you could use a single-quoted string literal - the escape sequence for a literal ' is simply '':

$infouptime = Invoke-SSHCommand -Index 0 -Command 'uptime -p | awk ''{print $2,$3,$4,$5,$6,$7}'''

Upvotes: 1

Related Questions