标题: [文本处理] 【已解决】wmic process得到的文本能否过滤提取内容? [打印本页]
作者: xzwcn 时间: 2020-10-19 16:07 标题: 【已解决】wmic process得到的文本能否过滤提取内容?
执行:- wmic process where name='w3wp.exe' get ProcessId,WorkingSetSize,Caption,CommandLine
复制代码
得到:- ProcessId WorkingSetSize
- Caption CommandLine ProcessId WorkingSetSize
- 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
- 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
作者: Batcher 时间: 2020-10-19 20:42
回复 1# xzwcn
这种格式可以吗:- @echo off
- setlocal enabledelayedexpansion
- for /f "delims=" %%i in ('wmic process where name^="explorer.exe" get ProcessId^,CommandLine^,WorkingSetSize^,Caption /value') do (
- set "str=%%i"
- if "!str:CommandLine=!" neq "%%i" (
- for /f tokens^=2^ delims^=^" %%a in ("%%i") do (
- echo,UserName=%%a
- )
- ) else (
- echo,%%i
- )
- )
- pause
复制代码
作者: xzwcn 时间: 2020-10-20 11:36
回复 2# Batcher
可以,谢谢!请问这个有办法合并到一行执行吗?
因为我是需要放到zabbix-agent里面执行的,它是一个配置文件,示例配置:- 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
作者: xzwcn 时间: 2020-10-20 11:38
回复 3# xzwcn - ### Option: UserParameter
- # User-defined parameter to monitor. There can be several user-defined parameters.
- # Format: UserParameter=<key>,<shell command>
- #
- # Mandatory: no
- # Default:
- # UserParameter=
-
- UserParameter=UserParameterWmic,wmic.exe process where name='w3wp.exe' get ProcessId,WorkingSetSize
复制代码
作者: xzwcn 时间: 2020-10-20 11:57
回复 3# xzwcn
单独创建一个批处理文件,然后调用也可以,如下:- UserParameter=UserParameterWmic,wmiclog.cmd
复制代码
得到的内容是乱码,把wmiclog.cmd另存为ANSI编码还是乱的
作者: xzwcn 时间: 2020-10-20 12:12
本帖最后由 xzwcn 于 2020-10-20 13:08 编辑
回复 5# xzwcn
把wmiclog.cmd改为完整路径或配置到环境变量就能正常运行
作者: xzwcn 时间: 2020-10-20 14:04
回复 2# Batcher
得到的结果:-
-
-
-
-
-
- Caption=w3wp.exe
-
-
- UserName=ycv3.0_www.yocity.cn_8090
-
- ProcessId=20504
-
-
- WorkingSetSize=55021568
-
-
-
-
-
-
-
-
- Caption=w3wp.exe
-
-
- UserName=5051
-
- ProcessId=13944
-
-
- WorkingSetSize=108449792
-
-
-
-
-
-
-
-
- Caption=w3wp.exe
-
-
- UserName=9090
-
- ProcessId=4144
-
-
- WorkingSetSize=102694912
-
-
-
-
-
-
-
-
- Caption=w3wp.exe
-
-
- UserName=5050
-
- ProcessId=18748
-
-
- WorkingSetSize=101281792
复制代码
同一批次的,可以去掉空行么?如下:-
- Caption=w3wp.exe
- UserName=ycv3.0_www.yocity.cn_8090
- ProcessId=20504
- WorkingSetSize=55021568
-
- Caption=w3wp.exe
- UserName=5051
- ProcessId=13944
- WorkingSetSize=108449792
-
- Caption=w3wp.exe
- UserName=9090
- ProcessId=4144
- WorkingSetSize=102694912
-
- Caption=w3wp.exe
- UserName=5050
- ProcessId=18748
- WorkingSetSize=101281792
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |