您必须在“ - ”运算符后面提供值表达式

时间:2016-02-22 15:00:23

标签: powershell cmd expression

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

我在cmd.exe中使用此命令

powershell -command "command"

但它给了我这个错误

At line:1 char:72
+ ... nt D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace REG_SZ, ...
+                                                                  ~
You must provide a value expression following the '-replace' operator.
At line:1 char:73
+ ... 5.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace REG_SZ,  } | F ...
+                                                            ~~~~~~
Unexpected token 'REG_SZ' in expression or statement.
At line:1 char:79
+ ... .txt -TotalCount 3)[-1] | Foreach-object {$_ -replace REG_SZ,  } | Fo ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:113
+ ... -object {$_ -replace REG_SZ,  } | Foreach-object {$_ -replace Gmail,  ...
+                                                                  ~
You must provide a value expression following the '-replace' operator.
At line:1 char:114
+ ... t {$_ -replace REG_SZ,  } | Foreach-object {$_ -replace Gmail,  } | s ...
+                                                             ~~~~~
Unexpected token 'Gmail' in expression or statement.
At line:1 char:119
+ ...  {$_ -replace REG_SZ,  } | Foreach-object {$_ -replace Gmail,  } | se ...
+                                                                 ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + 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"

2 个答案:

答案 0 :(得分:4)

您不能使用双引号来包装命令并在命令本身内部。 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"

答案 1 :(得分:2)

考虑使用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