Powershell 必须在'-';操作人员

Powershell 必须在'-';操作人员,powershell,cmd,expression,Powershell,Cmd,Expression,我在PowerShell中有一些代码,我需要在cmd.exe中使用它 powershell -command "command" 我正在cmd.exe中使用此命令 powershell -command "command" 但它给了我这个错误 第1行字符:72 + ... nt D:\15.txt-TotalCount 3)[-1]| Foreach对象{$\替换注册表{u SZ。。。 +

我在PowerShell中有一些代码,我需要在cmd.exe中使用它

powershell -command "command"
我正在cmd.exe中使用此命令

powershell -command "command"
但它给了我这个错误

第1行字符:72
+ ... nt D:\15.txt-TotalCount 3)[-1]| Foreach对象{$\替换注册表{u SZ。。。
+                                                                  ~
必须在“-replace”运算符后面提供值表达式。
第1行字符:73
+…5.txt-TotalCount 3)[-1]| Foreach对象{$|-replace REG|u SZ,}| F。。。
+                                                            ~~~~~~
表达式或语句中出现意外标记“REG_SZ”。
第1行字符:79
+…txt-TotalCount 3)[-1]| Foreach对象{$|-replace REG|u SZ,}| Fo。。。
+                                                                 ~
参数列表中缺少参数。
第1行字符:113
+…-对象{$\替换注册表{uSZ,}Foreach对象{$\替换Gmail。。。
+                                                                  ~
必须在“-replace”运算符后面提供值表达式。
第1行字符:114
+…t{$u-replace REG_SZ,}Foreach对象{$u-replace Gmail,}s。。。
+                                                             ~~~~~
表达式或语句中出现意外标记“Gmail”。
第1行字符:119
+…{$\-replace REG_SZ,}Foreach对象{$\-replace Gmail,}se。。。
+                                                                 ~
参数列表中缺少参数。
+CategoryInfo:ParserError:(:)[],ParentContainerErrorRecordException
+FullyQualifiedErrorId:ExpectedValueExpression
我的命令是:

powershell -command "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:\15.txt"

不能使用双引号将命令和命令本身括起来。
Cmd.exe/Powershell.exe
将在下一个双引号处结束命令,该双引号位于本例中的
“REG_SZ”
。请尝试在命令内使用单引号:

powershell -command "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace 'REG_SZ', ' '} | Foreach-object {$_ -replace 'Gmail', ' '} | set-content D:\15.txt"

考虑使用
powershell.exe-EncodedCommand
。若要使用该命令,您需要将要运行的命令设置为Base64,并在命令行上发送编码的字符串。但命令文本必须是Unicode。运行
powershell.exe/?
在最后给出了一个如何执行此操作的示例:

# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
这样做的好处是,您不必担心任何特殊字符,甚至是换行符被命令提示符解析器误解。您可以将代码写入脚本文件,然后转换整个代码:

# To use the -EncodedCommand parameter:
$command = (Get-Content C:\myscript.ps1) -join "`n"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

用你的命令:

$command = @"
(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:\15.txt
"@

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

这行吗?powershell-command“(获取内容D:\15.txt-TotalCount 3)[-1];Foreach对象{$\uU-replace\'REG\u SZ\',\“\”}Foreach对象{$\uU-replace\'Gmail\',\“\”}设置内容D:\15.txt“是的,在powershell中,但在cmdyes中不起作用,我忘了”“或者只是使用反勾字符(即“”)转义双引号)但考虑到他不需要变量扩展,这真是太难看了。:-)但是我可以使用双引号来包装命令,并在命令本身中使用()嗯?不能使用同一类型的引号来包装字符串,同时也在字符串中使用它(不转义)。同意。或者他可以为此创建一个脚本,并使用
-文件“c:\myscript.ps1”
。这是我喜欢的:-)所以我试试你的答案,打赌他会告诉我”无法识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在第1行char:1“@Yahya-uu,我需要查看您尝试的实际代码,以查看从何处获取该错误的实际代码”(获取内容D:\15.txt-TotalCount 3)[-1];Foreach对象{$\u-replace\'REG\u SZ\',\“\”}Foreach对象{$\u-replace\'Gmail\',\“\”}设置内容D:\15.txt | certutil-解码D:\15.txt D:\125.txt“\124;启动睡眠-s 60”到base64converter@Yahya-_不要逃避双引号