You must provide a value expression following the '-' operator
Asked Answered
C

2

5

I have a some code in PowerShell and I need to use it in cmd.exe

I am using this command in cmd.exe

powershell -command "command"

But it gives me this error

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

My command is :

powershell -command "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace "REG_SZ", " "} | Foreach-object {$_ -replace "Gmail", " "} | set-content D:\15.txt"
Chihli answered 22/2, 2016 at 15:0 Comment(3)
Does this work? powershell -command "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace \"REG_SZ\", \" \"} | Foreach-object {$_ -replace \"Gmail\", \" \"} | set-content D:\15.txt"Phototube
yes , in powershell but not in cmdChihli
yes , does work , i forget ""Chihli
F
6

You can't use doublequotes to wrap the command and inside the command itself. Cmd.exe/Powershell.exe will end the command at the next double quote which is at "REG_SZ" in this example. Try using single quotes inside the command:

powershell -command "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace 'REG_SZ', ' '} | Foreach-object {$_ -replace 'Gmail', ' '} | set-content D:\15.txt"
Fab answered 22/2, 2016 at 15:17 Comment(4)
Or just escape the double quotes using the backtick character (ie. `")Kehoe
But that's just ugly considering he doesn't need variable-expansion anyways. :-)Fab
but i can use doublequotes to wrap the command and inside the command itself by ()Chihli
Huh? You can't use the same type of quote to wrap a string while you're also using it inside the string (without escaping).Fab
W
2

Consider using powershell.exe -EncodedCommand. To use that, you would Base64 the command you want to run, and send the encoded string on the command line. The command text must be Unicode though. Running powershell.exe /? gives you an example at the very end of how to do this:

# 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

The nice thing about this is that you don't have to worry about any special characters, even newlines, being misinterpreted by the command prompt parser. You could write your code in a script file and then just convert that whole thing:

# 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

With your command:

$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
Willywillynilly answered 22/2, 2016 at 15:22 Comment(8)
Agreed. Or he could create a script for this and use -File "c:\myscript.ps1". That's what i prefer :-)Fab
so i try your answer bet he tell me "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:1"Chihli
@Yahya-_ I would need to see the actual code you tried to see where you're getting that errorWillywillynilly
the actual code "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace \"REG_SZ\", \" \"} | Foreach-object {$_ -replace \"Gmail\", \" \"} | set-content D:\15.txt | certutil -decode D:\15.txt D:\125.txt" | Start-Sleep -s 60" to base64 by converterChihli
@Yahya-_ Don't escape the double quotesWillywillynilly
I found the solution in the Supreme but i want try your idea my command : "(Get-Content D:\15.txt -TotalCount 3)[-1] | Foreach-object {$_ -replace 'REG_SZ', ' '} | Foreach-object {$_ -replace 'Gmail', ' '} | set-content D:\15.txt | certutil -decode D:\15.txt D:\125.txt" how i can write itChihli
thanks you command is right but in powershell but i want in cmdChihli
@Yahya-_ the snippet provided shows how to obtain $encodedCommand which is a string you can send to powershell.exe -encodedCommand from cmd.exe. It allows you to avoid worrying about quotes and other special characters. By all means use whichever works for you, I'm just offering an alternative that may work better in some situations, or may be more appealing to you.Willywillynilly

© 2022 - 2024 — McMap. All rights reserved.