1

我想获取某种格式的 Windows 修补程序列表,其输出可以用一些分隔符分隔。到目前为止,我找到了一个 wmic 命令,它给了我想要的输出,但问题是\s分隔符在这里不起作用。有没有办法可以放置一些,或任何其他字符,以后可以在java程序中使用它来获取各个列?

命令

wmic qfe get caption,csname,description,hotfixid,installedby,installedon

输出

Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
..
.

更新

我在尝试

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

但它给了我无效的 GET 表达式

C:\Users\Abhishek\Desktop>for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p
Invalid GET Expression.

这里有什么问题?这可能会为我解决问题。

更多更新

我什至尝试了以下命令,但这也不能解决空间问题

命令

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe list') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

输出

Caption,CSName,Description,FixComments,HotFixID,InstallDate,InstalledBy,InstalledOn,Name,ServicePackInEffect
http://go.microsoft.com/fwlink/?LinkId=161784,Abhishek,Update,KB971033,NT,AUTHOR,,Y\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2281679,Abhishek,Security,Update,KB2281679,NT,AUTHORITY\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2284742,Abhishek,Update,KB2284742,NT,AUTHORIT,,SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2286198,Abhishek,Security,Update,KB2286198,NT,AUTHORITY\SYSTEM,3/15/2012,
4

2 回答 2

5

正如我在 wmic 的结果中看到的那样,列至少2 个空格分隔。

Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012

因此,这可以通过使用\s{2,}正则表达式在 Java 中轻松解析:

String result = "...";
for (String line : result.split("\n")) {
     System.out.println("-> " + line);
     for (String column : line.split("\\s{2,}")) {
         System.out.println("  => [" + column.trim() + "]");
     }
 }

输出:

-> Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
  => [Caption]
  => [CSName]
  => [Description]
  => [HotFixID]
  => [InstalledBy]
  => [InstalledOn]
-> http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
  => [http://go.microsoft.com/fwlink/?LinkId=161784]
  => [Abhishek]
  => [Update]
  => [KB971033]
  => [NT AUTHORITY\SYSTEM]
  => [3/15/2012]
-> http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
  => [http://support.microsoft.com/?kbid=2032276]
  => [Abhishek]
  => [Security Update]
  => [KB2032276]
  => [NT AUTHORITY\SYSTEM]
  => [3/15/2012]

对于批处理方面,我无法帮助您,因为我对此一无所知:-)

编辑: 显然有一个开关应该生成一个CSV,你也可以很容易地用Java解析/format:csvwmic我无法让它在我的机器上工作,但值得关注。

于 2012-08-30T08:07:19.577 回答
0

我最近为这类事情做了一些事情,并设法让它使用以下语法。下面的代码片段。

FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ('WMIC /NODE:"%PCNAME%" PRODUCT GET 
IDENTIFYINGNUMBER^,INSTALLSOURCE^,NAME^,PACKAGECACHE^,PACKAGENAME^,VENDOR^,VERSION 
/FORMAT:CSV') DO (
 SET ELE1=%%a
 SET ELE2=%%b
 SET ELE3=%%c
 SET ELE4=%%d
 SET ELE5=%%e
 SET ELE6=%%f
 SET ELE7=%%g
 SET ELE8=%%h
 CALL :CheckIt
)
于 2020-06-19T14:53:37.473 回答