命令直接在Powershell中工作,但不';使用powershell通过linux系统执行时无法工作
在windows计算机中,如果打开powershell并编写此命令,它可以正常工作命令直接在Powershell中工作,但不';使用powershell通过linux系统执行时无法工作,powershell,Powershell,在windows计算机中,如果打开powershell并编写此命令,它可以正常工作 powershell-command“&{$p='It works';echo$p}” 这将输出,并按预期工作 但是如果我ssh使用linux机器进入同一台windows机器并尝试执行此命令,则会出现错误 命令: /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe-命令“&{$p='It works';echo$p}” 执行此操作时,会出现以
powershell-command“&{$p='It works';echo$p}”
这将输出,并按预期工作
但是如果我ssh使用linux机器进入同一台windows机器并尝试执行此命令,则会出现错误
命令:
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe-命令“&{$p='It works';echo$p}”
执行此操作时,会出现以下错误:
=It works : The term '=It works' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ &{='It works'; echo }
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (=It works:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
cmdlet Write-Output at command pipeline position 1
Supply values for the following parameters:
InputObject[0]:
我找了很多,但找不到背后的原因。如果有人能在这方面帮助我,我将不胜感激。您需要避开$
。像这样:
xxx@xxxxx:~$ pwsh -c "&{\$p='It works'; echo \$p}"
It works
从中添加示例,执行脚本块&
{}
(对于此特定情况),根本不需要对其执行字符转义:
xxx@xxxxx:~$ pwsh -c '$p="It works"; echo $p'
It works
您需要转义
$
。像这样:
xxx@xxxxx:~$ pwsh -c "&{\$p='It works'; echo \$p}"
It works
从中添加示例,执行脚本块&
{}(对于此特定情况),根本不需要对其执行字符转义:
xxx@xxxxx:~$ pwsh -c '$p="It works"; echo $p'
It works
删除符号
&
:注意,没有理由使用&{…}
来调用通过-command
(-c
)参数传递到PowerShell CLI的代码-直接使用..
。旧版本的错误建议需要&{…}
,但这已被更正/cc@AbrahamZinalaOn th Linux主机,是可执行文件powershell
或pwsh
?@litpwsh:ELF 64位LSB共享对象,x86-64,版本1(GNU/Linux),动态链接,解释器/lib64/ld-Linux-x86-64。因此,对于GNU/Linux 2.6.32,BuildID[sha1]=9c3feab2531f770c71d023f037758181701,拆下
将符号&
作为备用:请注意,没有理由使用&{…}
来调用通过-command
(-c
)参数传递到PowerShell CLI的代码-直接使用..
。旧版本的错误建议需要&{…}
,但这已被更正/cc@AbrahamZinalaOn th Linux主机,是可执行文件powershell
或pwsh
?@litpwsh:ELF 64位LSB共享对象,x86-64,版本1(GNU/Linux),动态链接,解释器/lib64/ld-Linux-x86-64。因此,对于GNU/Linux 2.6.32,BuildID[sha1]=9c3feab2531f770c71d023f037758181701,剥离