如何在wmic输出中设置列顺序?

时间:2012-04-20 09:26:17

标签: command-line batch-file wmic

我通常以这种方式使用wmic作为linux-ps-equivalent:

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

但输出列按字母顺序排序,所以我得到:

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

当我想要的是:

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

当命令行很长时,哪个更易读。

我正在考虑编写一个ps.bat来简化我的使用语法,因此非常欢迎任何用于后处理wmic输出的批处理脚本解决方案。

2 个答案:

答案 0 :(得分:1)

一个简单的批处理文件可以完成这项工作(仅适用于您的情况)。

通过搜索ProcessId确定第二列的起始位置,然后每行将重新排序

@echo off
setlocal EnableDelayedExpansion
set "first=1"
for /F "usebackq delims=" %%a in (`"wmic process where (name="cmd.exe") get processId, commandline"`) DO (
    set "line=%%a"
    if defined first (
        call :ProcessHeader %%a
        set "first="
        setlocal DisableDelayedExpansion
    ) ELSE (
        call :ProcessLine
    )
)
exit /b

:ProcessHeader line
set "line=%*"
set "line=!line:ProcessID=#!"
call :strlen col0Length line
set /a col1Start=col0Length-1
exit /b

:ProcessLine
setlocal EnableDelayedExpansion
set "line=!line:~0,-1!"
if defined line (
    set "col0=!line:~0,%col1Start%!"
    set "col1=!line:~%col1Start%!"
    echo(!col1!!col0!
)
Endlocal
exit /b

:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

答案 1 :(得分:1)

另一种选择是通过VBS直接访问WMI的 Win32_Process SQL表,而不使用WMIC。然后,您可以精确管理哪些列,列排序及其输出格式。

以下是CSV输出的VBS代码: 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

命令行用法: cscript //NoLogo processList.vbs

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

原始Java代码:http://www.rgagnon.com/javadetails/java-0593.html