您必须在“-”运算符之后提供一个值表达式
You must provide a value expression following the '-' operator
我在 PowerShell 中有一些代码,我需要在 cmd.exe
中使用它
我在cmd.exe
中使用这个命令
powershell -command "command"
但是它给我这个错误
At line:1 char:72
+ ... nt D:.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:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:.txt"
您不能使用双引号将命令和命令本身包裹起来。 Cmd.exe/Powershell.exe
将在本例中位于 "REG_SZ"
的下一个双引号处结束命令。尝试在命令中使用单引号:
powershell -command "(Get-Content D:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace 'REG_SZ', ' '} | Foreach-object {$_ -replace 'Gmail', ' '} | set-content D:.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:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:.txt
"@
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand
我在 PowerShell 中有一些代码,我需要在 cmd.exe
中使用它我在cmd.exe
中使用这个命令powershell -command "command"
但是它给我这个错误
At line:1 char:72
+ ... nt D:.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:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:.txt"
您不能使用双引号将命令和命令本身包裹起来。 Cmd.exe/Powershell.exe
将在本例中位于 "REG_SZ"
的下一个双引号处结束命令。尝试在命令中使用单引号:
powershell -command "(Get-Content D:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace 'REG_SZ', ' '} | Foreach-object {$_ -replace 'Gmail', ' '} | set-content D:.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:.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:.txt
"@
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand