[英]Command works directly in Powershell but doesn't work when executed through a linux system using powershell
In a windows machine, if I open powershell
and write this command, it works fine.在 windows 机器中,如果我打开
powershell
并编写此命令,它工作正常。
powershell -command "&{$p='It works'; echo $p}"
This outputs It works
as intended.这输出
It works
。
But if I ssh into the same windows machine using a linux machine and try to execute this command, this gives me an error.但是,如果我使用 linux 机器将ssh放入同一台 windows 机器并尝试执行此命令,这会给我一个错误。
The command:命令:
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -command "&{$p='It works'; echo $p}"
When I execute this, the following error appears:当我执行此操作时,会出现以下错误:
=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]:
I have searched a lot but couldn't find the reason behind it.我搜索了很多,但找不到背后的原因。 I'd really appreciate it if someone could help me regarding this.
如果有人可以帮助我解决这个问题,我将不胜感激。
You need to escape the $
.您需要转义
$
。 Like this:像这样:
xxx@xxxxx:~$ pwsh -c "&{\$p='It works'; echo \$p}"
It works
Adding the example from @mklement0 , executing &
a scriptblock { }
(for this specific case) is not needed at all to which character escaping wouldn't be needed either:添加来自@mklement0的示例,根本不需要执行
&
一个 scriptblock { }
(对于这种特定情况),也不需要字符 escaping :
xxx@xxxxx:~$ pwsh -c '$p="It works"; echo $p'
It works
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.