简体   繁体   English

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

[英]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.

相关问题 从.bat脚本运行Powershell命令不起作用,但是当我直接在命令行中键入它时它可以工作 - Running Powershell command from .bat script doesn't work, however it works when I type it directly in Command Line Powershell 命令手动工作,但在 Jenkins 中执行时不起作用 - Powershell command works manually, but does not work when executed in Jenkins 为什么不直接使用powershell中的命令工作? - why doesn't splat work directly with command in powershell? 当由PHP执行时,PowerShell脚本不起作用 - PowerShell script doesn't work when executed by PHP 从VBA调用时,PowerShell命令不起作用,但其他方式有效 - PowerShell command doesn't work when called from VBA, but otherwise works 使用字符串执行命令在powershell中不起作用 - Executing a command with string doesn't work in powershell Chef Powershell命令不起作用 - chef powershell command doesn't work dotnet 命令在 powershell 中不起作用 - dotnet command doesn't work from powershell powershell 命令成批处理 - 它不起作用 - powershell command into batch - it doesn't work 为什么有时在 PowerShell 的“python -c [command]”(以及命令提示符)中使用单引号将命令括起来时不起作用? - Why doesn't it work sometimes when using single quotes to surround the command in `python -c [command]` in PowerShell (and in command prompt as well)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM