定时任务bat获取 Windows的进程 内存和内存使用率 CPU CPU 使用率 磁盘空间 磁盘使用率
时间: 2023-09-13 20:12:28 浏览: 213
你可以创建一个定时任务,在特定的时间间隔内运行命令以获取Windows的进程内存和内存使用率,CPU和CPU使用率,以及磁盘空间和磁盘使用率。
以下是一个示例定时任务的批处理脚本:
```
@echo off
setlocal EnableDelayedExpansion
set logDir=C:\Logs
set logFile=%logDir%\system_info.csv
if not exist %logDir% mkdir %logDir%
if not exist %logFile% echo "Timestamp","Process Name","PID","Memory Usage (KB)","Memory Usage (%)","CPU Usage (%)" >> %logFile%
set timestamp=%date:~4,2%/%date:~7,2%/%date:~10,4% %time:~0,2%:%time:~3,2%:%time:~6,2%
for /f "usebackq skip=1 tokens=2,3,4,5,6 delims=," %%a in (`tasklist /fo csv /nh`) do (
set procName=%%~a
set pid=%%~b
set memUsage=%%~c
set /a memUsagePct=memUsage*100/totalMem
for /f "usebackq skip=1 tokens=2 delims== " %%x in (`typeperf "\Process(%%a)\%% Processor Time" -sc 1`) do (
set cpuUsage=%%x
echo "%timestamp%","!procName!","!pid!","!memUsage!","!memUsagePct!","!cpuUsage!" >> %logFile%
)
)
set totalMem=0
for /f "usebackq skip=1 tokens=1,2,3 delims=," %%a in (`wmic memorychip get capacity /format:csv`) do (
set /a totalMem+=%%~a/1024
)
set cpuLoad=0
for /f "usebackq skip=1 tokens=2 delims== " %%x in (`typeperf "\Processor(_Total)\%% Processor Time" -sc 1`) do (
set cpuLoad=%%x
)
set diskUsage=
for /f "usebackq skip=1 tokens=1,2,3 delims=," %%a in (`wmic logicaldisk get caption,freespace,size /format:csv`) do (
set /a diskUsage=100-%%~b*100/%%~c
echo "%timestamp%","%%~a","%%~c","%%~b","!diskUsage!" >> %logFile%
)
exit /b
```
该脚本将获取有关正在运行的进程的信息、系统内存使用情况、CPU使用情况和系统磁盘使用情况的信息,并将其写入一个名为 system_info.csv 的 CSV 文件中。在运行脚本之前,请确保设置 logDir 变量以指定要保存日志文件的目录。
你可以将该脚本添加到Windows的计划任务中,以在特定时间间隔内运行它。
阅读全文