Getting processor, using memory and free disk space with powershell

Could you help me combine these three scripts into a format similar to this

ComputerName CPUUsageAverage MemoryUsage C PercentFree xxx 12 50% 30% 

The way I do this is: Get-content '.\servers.txt' | Get-CPUusageAverage Get-content '.\servers.txt' | Get-CPUusageAverage

Here are the scenarios:

CPU

 Function Get-CPUusageAverage { $input|Foreach-Object{Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average} } 

Memory

  Function get-MemoryUsage { $input|Foreach-Object{ gwmi -Class win32_operatingsystem -computername $_ | Select-Object @{Name = "MemoryUsage"; Expression = { "{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) } } } } 

C PercentFree

 Function get-CPercentFree { $input|ForEach-Object{ Get-WmiObject -Class win32_Volume -ComputerName $_ -Filter "DriveLetter = 'C:'" | Select-object @{Name = "C PercentFree"; Expression = { "{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) } } } } 
+4
source share
3 answers

At first I would not use $ input. You can simply combine the queries into one function, which then displays pscustomobject with data for each computer, for example:

 function Get-ComputerStats { param( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [ValidateNotNull()] [string[]]$ComputerName ) process { foreach ($c in $ComputerName) { $avg = Get-WmiObject win32_processor -computername $c | Measure-Object -property LoadPercentage -Average | Foreach {$_.Average} $mem = Get-WmiObject win32_operatingsystem -ComputerName $c | Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)} $free = Get-WmiObject Win32_Volume -ComputerName $c -Filter "DriveLetter = 'C:'" | Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100)} new-object psobject -prop @{ # Work on PowerShell V2 and below # [pscustomobject] [ordered] @{ # Only if on PowerShell V3 ComputerName = $c AverageCpu = $avg MemoryUsage = $mem PercentFree = $free } } } cat '.\servers.txt' | Get-ComputerStats | Format-Table 
+8
source

What about:

 cat '.\servers.txt' | % {select -property @{'n'='CPUUsageAverage';'e'={$_ | Get-CPUUsageAverage}},@{'n'='Memory Usage';'e'={$_ | Get-MemoryUsage}},@{'n'=C PercentFree';'e'={$_ | Get-CPercentFree}}} 
+2
source

Apologizes above my previous post (edited), here's the script I use to get the average free space of the CPU, Mem, and C Drive that are generated in the HTML file

 $ServerListFile = "D:\serverList.txt" $ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue $Result = @() ForEach($computername in $ServerList) { $AVGProc = Get-WmiObject -computername $computername win32_processor | Measure-Object -property LoadPercentage -Average | Select Average $OS = gwmi -Class win32_operatingsystem -computername $computername | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }} $vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" | Select-object @{Name = "C PercentFree"; Expression = {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100) } } $result += [PSCustomObject] @{ ServerName = "$computername" CPULoad = "$($AVGProc.Average)%" MemLoad = "$($OS.MemoryUsage)%" CDrive = "$($vol.'C PercentFree')%" } $Outputreport = "<HTML><TITLE> Server Health Report </TITLE> <BODY background-color:peachpuff> <font color =""#99000"" face=""Microsoft Tai le""> <H2> Server Health Report </H2></font> <Table border=1 cellpadding=0 cellspacing=0> <TR bgcolor=gray align=center> <TD><B>Server Name</B></TD> <TD><B>Avrg.CPU Utilization</B></TD> <TD><B>Memory Utilization</B></TD> <TD><B>Drive C Free Space</B></TD> </TR>" Foreach($Entry in $Result) { if(($Entry.CpuLoad) -or ($Entry.memload) -ge "80") { $Outputreport += "<TR bgcolor=white>" } else { $Outputreport += "<TR>" } $Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.CDrive)</TD></TR>" } $Outputreport += "</Table></BODY></HTML>" } $Outputreport | out-file "D:\Result $(Get-Date -Format yyy-mm-dd-hhmm).htm" 
-one
source

Source: https://habr.com/ru/post/1495618/


All Articles