找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 25744|回复: 6

[文本处理] 【已解决】wmic process得到的文本能否过滤提取内容?

[复制链接]
发表于 2020-10-19 16:07:50 | 显示全部楼层 |阅读模式
执行:
  1. wmic process where name='w3wp.exe' get ProcessId,WorkingSetSize,Caption,CommandLine
复制代码
得到:
  1.                                                                                                                                                                                                                                                        ProcessId  WorkingSetSize  
  2. Caption   CommandLine                                                                                                                                                                                                                                                                ProcessId  WorkingSetSize  
  3. w3wp.exe  c:\windows\system32\inetsrv\w3wp.exe -ap "5050" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipmdc50f55a-6336-4f59-8b39-c0d76a89131b -h "C:\inetpub\temp\apppools\5050\5050.config" -w "" -m 0 -t 20 -ta 0                                                                 16644      114728960      
  4. w3wp.exe  c:\windows\system32\inetsrv\w3wp.exe -ap "5051" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipme8e08e32-0c70-47af-8695-fc902c833a22 -h "C:\inetpub\temp\apppools\5051\5051.config" -w "" -m 0 -t 20 -ta 0                                                                 13944      141348864      
复制代码
可以对得到的结果进行替换吗?得到类似如下的结果:[code]                                                                                                                                                                                                                                 
Caption   UserName   ProcessId  WorkingSetSize                                                                                                                                                                                                                                                           
w3wp.exe  5050        16644         114728960      
w3wp.exe  5051           13944         141348864

即:列名变化:CommandLine--->UserName,
列UserName的值取原“c:\windows\system32\inetsrv\w3wp.exe -ap”后第一个引号里面的内容,如原来是c:\windows\system32\inetsrv\w3wp.exe -ap "5050" .....只取5050
发表于 2020-10-19 20:42:12 | 显示全部楼层
回复 1# xzwcn


这种格式可以吗:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%i in ('wmic process where name^="explorer.exe" get ProcessId^,CommandLine^,WorkingSetSize^,Caption /value') do (
  4.     set "str=%%i"
  5.     if "!str:CommandLine=!" neq "%%i" (
  6.         for /f tokens^=2^ delims^=^" %%a in ("%%i") do (
  7.             echo,UserName=%%a
  8.         )
  9.     ) else (
  10.         echo,%%i
  11.     )
  12. )
  13. pause
复制代码

评分

参与人数 1技术 +1 收起 理由
xzwcn + 1 感谢分享

查看全部评分

 楼主| 发表于 2020-10-20 11:36:17 | 显示全部楼层
回复 2# Batcher
可以,谢谢!请问这个有办法合并到一行执行吗?
因为我是需要放到zabbix-agent里面执行的,它是一个配置文件,示例配置:
  1. UserParameter=UserParameterWmic,wmic.exe process where name='w3wp.exe' get ProcessId,WorkingSetSize
复制代码
批处理部分为:wmic.exe process where name='w3wp.exe' get ProcessId,WorkingSetSize

参考文章:
Zabbix监控在windows的进程(非进程数)_运维_乐维 的博客-CSDN博客
https://blog.csdn.net/weixin_43631631/article/details/105575412
 楼主| 发表于 2020-10-20 11:38:45 | 显示全部楼层
回复 3# xzwcn
  1. ### Option: UserParameter
  2. #        User-defined parameter to monitor. There can be several user-defined parameters.
  3. #        Format: UserParameter=<key>,<shell command>
  4. #
  5. # Mandatory: no
  6. # Default:
  7. # UserParameter=

  8. UserParameter=UserParameterWmic,wmic.exe process where name='w3wp.exe' get ProcessId,WorkingSetSize
复制代码
 楼主| 发表于 2020-10-20 11:57:51 | 显示全部楼层
回复 3# xzwcn

单独创建一个批处理文件,然后调用也可以,如下:
  1. UserParameter=UserParameterWmic,wmiclog.cmd
复制代码
得到的内容是乱码,把wmiclog.cmd另存为ANSI编码还是乱的
 楼主| 发表于 2020-10-20 12:12:27 | 显示全部楼层
本帖最后由 xzwcn 于 2020-10-20 13:08 编辑

回复 5# xzwcn
把wmiclog.cmd改为完整路径或配置到环境变量就能正常运行
 楼主| 发表于 2020-10-20 14:04:08 | 显示全部楼层
回复 2# Batcher

得到的结果:
  1.        





  2. Caption=w3wp.exe


  3. UserName=ycv3.0_www.yocity.cn_8090

  4. ProcessId=20504


  5. WorkingSetSize=55021568








  6. Caption=w3wp.exe


  7. UserName=5051

  8. ProcessId=13944


  9. WorkingSetSize=108449792








  10. Caption=w3wp.exe


  11. UserName=9090

  12. ProcessId=4144


  13. WorkingSetSize=102694912








  14. Caption=w3wp.exe


  15. UserName=5050

  16. ProcessId=18748


  17. WorkingSetSize=101281792
复制代码
同一批次的,可以去掉空行么?如下:
  1.        
  2. Caption=w3wp.exe
  3. UserName=ycv3.0_www.yocity.cn_8090
  4. ProcessId=20504
  5. WorkingSetSize=55021568

  6. Caption=w3wp.exe
  7. UserName=5051
  8. ProcessId=13944
  9. WorkingSetSize=108449792

  10. Caption=w3wp.exe
  11. UserName=9090
  12. ProcessId=4144
  13. WorkingSetSize=102694912

  14. Caption=w3wp.exe
  15. UserName=5050
  16. ProcessId=18748
  17. WorkingSetSize=101281792
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 23:08 , Processed in 0.031987 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表