1

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 :)

4

1 回答 1

1

PowerShell 使用反引号`作为可扩展字符串中的转义字符:

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

或者,由于您实际上没有任何需要扩展的变量,您可以使用单引号字符串文字 - 文字的转义序列'很简单''

$infouptime = Invoke-SSHCommand -Index 0 -Command 'uptime -p | awk ''{print $2,$3,$4,$5,$6,$7}'''
于 2021-06-26T19:31:13.693 回答