9

Im trying to write a very brief powershell script that runs a few commands, pipes their output to a text file, and then does a search against a keyword.

I cant figure out what to change however for this line:

wmic service get name, startname | out-File "$pwd\admin\wmic.txt"

WMIC.exe : Invalid GET Expression.
At \\test.ps1:7 char:5
+ wmic <<<<  service get name startname | out-File "$pwd\admin\wmic.txt"
+ CategoryInfo          : NotSpecified: (Invalid GET Expression.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

I believe the error is generated from the ',' as powershell uses the comma to create an array. Any thoughts or suggestions?

Thanks

Jingles177
  • 499
  • 2
  • 7
  • 16

3 Answers3

14
wmic service get name"," startname| out-File "$pwd\admin\wmic.txt"

This is easy to resolve comma, no change at all.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Raymond Cheng
  • 156
  • 1
  • 3
8

Beginning with PowerShell 3.0 there is the new escaping operator --% available (see magic parameter in the release notes):

wmic --% service get name, startname | out-File "$pwd\admin\wmic.txt"

This operator is known as the stop-parsing operator, and it instructs PowerShell to stop interpreting input from that point forward as PowerShell commands or expressions (see docs here).

pb2q
  • 58,613
  • 19
  • 146
  • 147
Cwt
  • 8,206
  • 3
  • 32
  • 27
  • Thanks for this @sevenforce. To be pedantic, an equivalent command is `wmic service get --% name, start name | out-File ...` because it's the commas and whitespace that start to cause the problem – pb2q Jun 03 '21 at 18:48
1

The answer to this question lies in the use of the Start-Process cmdlet. This will make interpretation of command line arguments much easier, since you can also construct the command line arguments ahead of time, store them in a variable, and then reference them using the variable name.

$WmicArgs = 'service get name, startname';
Start-Process -FilePath wmic.exe -ArgumentList $WmicArgs;

But, why are you even using WMIC.exe?

There are easier ways to work with WMI, like using the PowerShell type accelerators [wmi], [wmiclass], [wmisearcher], and the Get-WmiObject cmdlet.

  • Im using WMIC because I need to find all of the locations where "administrator" is used. I planned to get the output of wmic service and store that into a text file. Then from here do a search on the word administrator and return that whole line to a new file if possible. – Jingles177 Jun 22 '12 at 18:44
  • What information exactly do you need from WMI? What do you mean by "locations where Administrator is used?" I might be able to help you further. –  Jun 22 '12 at 19:58
  • I just needed to see what name and startname are running for the WMIC. Its a little vague cause im not sure how to explain what I am attempting to do. I ended up going with a simple batch script to run the above command, pipe to a file and then parse that file for the keyword, and then finally send that information to a new file for later use. Thank you for your help however. – Jingles177 Jun 22 '12 at 21:24