繁体   English   中英

命令直接在 Powershell 中工作,但在使用 powershell 通过 linux 系统执行时不起作用

[英]Command works directly in Powershell but doesn't work when executed through a linux system using powershell

在 windows 机器中,如果我打开powershell并编写此命令,它工作正常。

powershell -command "&{$p='It works'; echo $p}"

这输出It works

但是,如果我使用 linux 机器将ssh放入同一台 windows 机器并尝试执行此命令,这会给我一个错误。

命令:

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command "&{$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

添加来自@mklement0的示例,根本不需要执行&一个 scriptblock { } (对于这种特定情况),也不需要字符 escaping :

xxx@xxxxx:~$ pwsh -c '$p="It works"; echo $p'
It works

暂无
暂无

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

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