使用powershell获取进程的CPU内存状态,并导出到excel。
在Windows中,监视某个进程的状态,可以使用 perfmon 性能监视器。但是他的报表无法导出到excel,经过一番折腾之后,找到了powershell的解决方案,当然可能不如c#+.net framwork优秀。
现在使用powershell来监视进程的状态,首先需要powershell 4.0及以上。
查看版本的方式:打开powershell 运行
$psversiontable.psversion
如果是win7系统可以升级到4.0或者5.0
powershell 5.0 下载链接:https://www.microsoft.com/en-us/download/details.aspx?id=50395
现在创建一个powershell脚本文件:例如process.ps1
编辑文件,首先定义一些参数
$appname = "tim" #进程简称 $xc = 4 #CPU线程数 $line = 10#行数 $zhouqi = 2 #取样周期 $data = 1..$line $path=[Environment]::GetFolderPath("Desktop")#保存路径,此路径为桌面
进程简称是为了系列进程考虑,也可以使用全称;行数就是取样的次数,行数乘以采样周期就是整个的监视时间。保存路径可以自定,此处使用桌面
另外一个主要的方法是监视进程
1 function getCPU ([string]$iProcess) { 2 $z = "*$iProcess*" #带'*'用于模糊查找,比如一些列进程名字不同,eg:MicrosoftEdge,MicrosoftEdgeCP 3 $process1 = Get-Process $z 4 5 $a = $process1.CPU 6 sleep $zhouqi 7 $process2 = Get-Process $z 8 $b = $process2.CPU 9 $d = $process2.ProcessName 10 $f = $process2.WS 11 12 $c = New-Object object 13 Add-Member -InputObject $c -Name prea -Value "$a" -MemberType NoteProperty; 14 Add-Member -InputObject $c -Name preb -Value "$b" -MemberType NoteProperty; 15 Add-Member -InputObject $c -Name pro -Value "$d" -MemberType NoteProperty; 16 Add-Member -InputObject $c -Name mem -Value "$f" -MemberType NoteProperty; 17 18 return $c 19 20 }
这个函数的返回值 是一个数组对象,包含采样前的CPU时间,采样后的CPU时间,以及内存和进程全名。
下面使用定义的getCPU函数获取进程的信息并处理
function getData($a, $b) { $dataobject = New-Object object $time=getDate Add-Member -InputObject $dataobject -Name time -Value " $time " -MemberType NoteProperty; Add-Member -InputObject $dataobject -Name mem -Value " $a " -MemberType NoteProperty; Add-Member -InputObject $dataobject -Name pre -Value " $b " -MemberType NoteProperty; return $dataobject } for ($m = 0; $m -le ($line - 1); $m++) { $return = (getCPU $appname) $arraya = $return.prea $arrayb = $return.preb $pro = ($return.pro -split " ")[0] $mem = ($return.mem -split " ") $newarraya = ($arraya -split " ") $newarrayb = ($arrayb -split " ") $pre = 0..($newarraya.Count - 1) for ($i = 0; $i -le ($newarraya.Count - 1); $i++) { $pre[$i] = ($newarrayb[$i] - $newarraya[$i]) / $zhouqi * 100 / $xc; } $preT = 0 for ($n = 0; $n -le ($pre.Count - 1); $n++) { $tmp = $pre[$n] $preT += $tmp } $memT = 0 for ($n = 0; $n -le ($mem.Count - 1); $n++) { $tmp = $mem[$n] $memT += $tmp } $data[$m] = getData $memT $preT $data[$m] #可注释掉,用来显示当前内存和CPU使用率 }
这段代码主要解决的是系列进程问题,比如浏览器多开,VS code的多进程 最终获得$data就是我们用于excel导出的数据
excel导出需要完整版office提供dom组件。
验证方法
$excel = New-Object -ComObject Excel.Application
未报错即可
使用excel导出,首先需要利用com创建一个对象,然后添加sheet等;
$excel = New-Object -ComObject Excel.Application $workbook = $excel.Workbooks.add() $sheet = $workbook.worksheets.Item(1) $workbook.WorkSheets.item(1).Name = $pro
然后利用之前的 $data数据进行excel导出,网上很多,这里也不多说了
1 $x = 2 2 3 $lineStyle = "microsoft.office.interop.excel.xlLineStyle" -as [type] 4 $colorIndex = "microsoft.office.interop.excel.xlColorIndex" -as [type] 5 $borderWeight = "microsoft.office.interop.excel.xlBorderWeight" -as [type] 6 $chartType = "microsoft.office.interop.excel.xlChartType" -as [type] 7 8 for ($b = 1 ; $b -le 3 ; $b++) { 9 $sheet.cells.item(1, $b).font.bold = $true 10 $sheet.cells.item(1, $b).borders.LineStyle = $lineStyle::xlDashDot 11 $sheet.cells.item(1, $b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic 12 $sheet.cells.item(1, $b).borders.weight = $borderWeight::xlMedium 13 } 14 15 $sheet.cells.item(1, 1) = "title time" 16 $sheet.cells.item(1, 2) = "WS memory(MB)" 17 $sheet.cells.item(1, 3) = "CPU(%)" 18 19 20 foreach ($process in $data) { 21 $sheet.cells.item($x, 1) = $process.time 22 $sheet.cells.item($x, 2) = $process.mem/1024/1024 23 $sheet.cells.item($x, 3) = $process.pre 24 25 $x++ 26 } 27 28 $range = $sheet.usedRange 29 $range.EntireColumn.AutoFit() | out-null 30 $excel.Visible = $true 31 $filename= $appname+'-'+(Get-Date -Format 'MMddhhmm')+'.xlsx' #excel文件名 32 $excel.ActiveWorkBook.SaveAs($path+'/'+$filename)#保存excel 33 #$excel.quit()
最后将会获得一个三列$line+1行的excel 可以使用excel的图标和函数功能来分析数据。
ps:语文十几年没及格的,随便写写!
附上github地址:https://github.com/aswezxqcc/ProcessesCount
分享一点,获得一点。