利用PowerShell写的一个CPU监控小程序
本帖最后由 LiGuan 于 2023-5-18 15:34 编辑新人第一次发帖,业务部门需要,所以写的一个CPU监控小程序,有窗口显示,同时会在当前目录生成日志,有需要的自取,复制代码,TXT另存为xx.ps1即可。
# powershell -noprofile Set-ExecutionPolicy Unrestricted
# 如果程序运行失败,请以管理员运行CMD,执行第一段#号后面的命令!
$counter = New-Object Diagnostics.PerformanceCounter
$counter.CategoryName = "Processor"
$counter.CounterName = "% Processor Time"
$counter.InstanceName = "_Total"
while ($true)
{
$value = $counter.NextValue()
$result = "{0:N0}" -f $value
Write-Host " CPU Utilization: $result%"
Write-Host " Date: $(Get-date)"
Write-Host
$path = Split-Path -Parent $MyInvocation.MyCommand.Definition
Write-Output "CPU Utilization: $($result)%" >> "$path\CPU Utilization.txt"
Write-Output "Date: $(Get-date)" >> "$path\CPU Utilization.txt"
Write-Output "" >> "$path\CPU Utilization.txt"
Sleep 5
#修改Sleep的赋值,可以更改监控频率!
} 和任务管理器里面显示的差很多是正常的吗?
页:
[1]