Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set column-order in wmic output?

I use wmic mostly as a linux-ps-equivalent in this way:

wmic process where (name="java.exe") get processId, commandline

But the output columns are ordered alphabetically so I get:

CommandLine                            ProcessId
java -cp ... some.Prog arg1 arg2 ...   2345
java -cp ... other.Prog arg1 arg2 ...  3456

When what I want is:

ProcessId  CommandLine
2345       java -cp .... some.Prog arg1 arg2 ...
3456       java -cp .... other.Prog arg1 arg2 ...

Which would be much more readable when the commandline is long.

I'm thinking of writing a ps.bat to simplify the syntax for my use, so any batch script solutions for post-processing the wmic output are very welcome.

like image 512
Superole Avatar asked Oct 30 '25 05:10

Superole


1 Answers

Another option is directly accessing the Win32_Process SQL table of the WMI via VBS, without using WMIC. Then you can manage exactly which columns, column ordering and their output format.

Here's the VBS code for CSV output: processList.vbs

' === Direct access to Win32_Process data ===
' -------------------------------------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set service = locator.ConnectServer()
Set processes = service.ExecQuery ("select ProcessId,CommandLine,KernelModeTime,UserModeTime from Win32_Process")

For Each process in processes
   Return = process.GetOwner(strNameOfUser) 
   wscript.echo process.ProcessId & "," & process.KernelModeTime & "," & process.UserModeTime & "," & strNameOfUser & "," & process.CommandLine
Next

Set WSHShell = Nothing

Command line usage: cscript //NoLogo processList.vbs

Win32_Process column list: http://msdn.microsoft.com/en-gb/library/windows/desktop/aa394372(v=vs.85).aspx

Original Java code here: http://www.rgagnon.com/javadetails/java-0593.html

like image 179
pds Avatar answered Oct 31 '25 20:10

pds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!