PowerShell巡检主机获取CPU占用、内存使用、硬盘情况的脚本

Windows底下用PowerShell写的获取CPU核数、占用率;内存可用内存大小(GB)、使用率;硬盘总空余大小(GB),使用率


$Server = $env:computername
#server's CPU Mem Hardinfor 
$IpAdd = (Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').ipaddress[0]
$SystemVersion = Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption
$cpu = gwmi -computername $Server win32_Processor 
$men = gwmi -computername $Server win32_OperatingSystem 
$Disks = gwmi -Computer: $Server win32_logicaldisk -filter "drivetype=3" 
$Havecpu = "{0:0.0} %" -f $cpu.LoadPercentage 
$Allmen = "{0:0.0} MB" -f ($men.TotalVisibleMemorySize  / 1KB) 
$Freemen = "{0:0.0} MB" -f ($men.FreePhysicalMemory  / 1KB) 
$Permem =  "{0:0.0} %" -f ((($men.TotalVisibleMemorySize-$men.FreePhysicalMemory)/$men.TotalVisibleMemorySize)*100) 

Write-Host "Computer:$Server|"`r`n
Write-Host "SystemVersion:$SystemVersion|"`r`n
Write-Host "Ipaddress:$IpAdd|"`r`n
Write-Host "CPU: $Havecpu|"`r`n
Write-Host "TotalMem:$Allmen|"`r`n 
Write-Host "FreeMem:$Freemen|"`r`n
Write-Host "UsedMem:$Permem|"`r`n




将之封闭为BAT运行,并将结果COPY至统一机器共享内:

powershell .\GetCPUMem.ps1 > \\XXX\Iperf\log\%computername%.txt

 

下一步,必然是通过BAT或PS在每个客户机上建立相应的SCHEDULE TASK啦。

然后,集中数据,一方面将数据导入SQL用于以后分析,通过WEB即时访问,固定时间通过邮件发送REPORT。

https://blog.csdn.net/sinat_28371057/article/details/91376741

posted @ 2022-03-15 16:28  hanfe1  阅读(1936)  评论(0编辑  收藏  举报