5

Could you help me combine these three scripts to a format that's like this

ComputerName     CPUUsageAverage    MemoryUsage    C PercentFree
xxx                           12            50%              30%

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

Here are the scripts:

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) } }
 } 
 }

3 Answers 3

10

First I would avoid using $input. You can just combine the queries into single function that then outputs a pscustomobject with the data for each computer e.g.:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Any idea what's causing the output of the CPU to be like @{Average=46} and the ComputerName column on the 3rd column?
Vaguaries of the hashtable approach. If you are on V3, you can change the new-object ... line to [pscustomobject] [ordered] @{ to fix the order of the properties.
0

How about:

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

1 Comment

How do I execute this? Should I combine all three scripts to a single powershell file?
-1

Apologies above my previous post (Edited this), here's a script I am using to get the average of CPU, Mem, and C Drive free space which generate to an 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"

4 Comments

Post a new question. Don't post as an answer, as... it's not an answer. You're also posting to a question asked, and answered, 3 years ago.
Sorry bout that, David and RamenChef, I'll take note of it. Anyway, I've edited my post, showing as answer now. hope it's all good. Thanks for the advise. By the way David, you've mentioned that it has been answered 3 years ago, would you be able to point to the link of that answer? Thanks
@Gibo The answer is posted to the exact same question you posted this to. With a bunch of upvotes and an accept from the OP.
@David, With the one you are referring to answer, If I have analyzed it correctly, i see it the same function as the one I have. What if I have multiple servers, having different averages, and in the end of the table, I would like to get the total average of this data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.