简体   繁体   English

如何在 Invoke-SSHCommand 中使用字符“$”而不会出错

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

I try to get information from a Linux server in a powershell script我尝试使用 powershell 脚本从 Linux 服务器获取信息

I use the Windows forms and i get the informations in a RichTextBox.我使用 Windows 表单,并在 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我尝试设置\\$'e'$'"+chr(38)+"但没有任何效果

If someone can help me, it will be nice :)如果有人可以帮助我,那就太好了:)

PowerShell uses the backtick ` as the escape character in expandable strings: PowerShell 使用反引号`作为可扩展字符串中的转义字符:

$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}'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM