NIC Utilisation

Hi Gents, i have the below one liner which kind of gets me the info i need (at least current state measurement)

Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkInterface -ComputerName eurxmbs04 | Where-Object {$_.Name -like "*HP*"} | format-table Name,BytesTotalPersec, CurrentBandwidth,PacketsPersec

it produces a table like below

Name BytesTotalPersec CurrentBandwidth PacketsPersec


HP Ethernet 1Gb 4-port 331i Adapter 32888336 1000000000 27643
HP Ethernet 1Gb 4-port 331i Adapter _2 13934503 1000000000 12983
HP Ethernet 1Gb 4-port 331i Adapter _3 16849901 1000000000 18291
HP Ethernet 1Gb 4-port 331i Adapter _4 42856234 1000000000 35214
HP Network Team _1 30168422 2000000000 19444

However what i would like to do is measure the % similar to what task manager produces and put this at the end of the table, any pointers would be much appreciated

ended up with the below


$colInterfaces = Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkInterface -ComputerName eurxmbs04 | Where-Object {$_.Name -eq "HP Ethernet 1Gb 4-port 331i Adapter" -or $_.Name -eq "HP Ethernet 1Gb 4-port 331i Adapter _4" -or $_.Name -eq "HP Network Team _1"} 



   foreach ($interface in $colInterfaces) {

        $bitsPerSec = $interface.BytesTotalPersec * 8
        $totalBits = $interface.CurrentBandwidth
         if ($totalBits -gt 0) {
         $result = (( $bitsPerSec / $totalBits) * 100)
         $interface2 = $interface | select name -ExpandProperty name 
         $interface3 = $interface | select pscomputername -ExpandProperty pscomputername

         $text =  "$interface2 on $interface3
         
         Bandwidth utilized:  $result %

         

         " 
         
         if ($result -gt 60) {     
         
##Variables

   
    $smtpServer = "smtp.test.com"
    $From = "NIC-Monitor@test.com"
    $SubjectCPY = "NIC $interface2 on $interface3 is above 60% Threshold"
    $Body = "$text"
    $prior = "mark.prior@test.com"

#Email Content
## -cc $mail will mail user account
Send-Mailmessage -smtpServer $smtpServer -from $from -to $Prior -subject $subjectCPY -priority High -Body $Body -BodyAsHtml}
         
         
         
         }}
         

You can simplify your code quite a bit by utilising the pipeline

You’ll need to add your original where-object filter back in and change the threshold in the last where

Get-WmiObject -ClassName Win32_PerfFormattedData_Tcpip_NetworkInterface |
where CurrentBandwidth -gt 0 | 
foreach {
   
   $props = [ordered]@{
     Name = $psitem.Name
     Computer = $psitem.PSComputerName
     PercUtilisation = (($psitem.BytesTotalPersec * 8) / $psitem.CurrentBandWidth) * 100
   }
   
   New-Object -TypeName PSObject -Property $props

 } |
 where PercUtilisation -gt 0.5 |
 foreach {
   $text =  @"
   $($_.Name) on $($_.Computer)
         
   Bandwidth utilized:  $($_.PercUtilisation) %
   
   
"@

 $smtpServer = 'smtp.test.com'
 $From = 'NIC-Monitor@test.com'
 $SubjectCPY = "NIC $($_.Name) on $($_.Computer) is above 60% Threshold"
 $prior = 'mark.prior@test.com'
 Send-Mailmessage -smtpServer $smtpServer -from $from -to $Prior -subject $subjectCPY -priority High -Body $text -BodyAsHtml

 }

Thanks richard

Actually another question, if it detects utilisation on 1 NIC over the threshold how can i get the email to encorp the other results that may or may not be over the threshold ? i.e how would i get the below

"Subject: NIC HP Ethernet 1Gb 4-port 331i Adapter _4 on EURXMBS04 is above 60% Threshold

HP Ethernet 1Gb 4-port 331i Adapter _4 on EURXMBS04 Bandwidth utilized: 67.128768 %

HP Ethernet 1Gb 4-port 331i Adapter on EURXMBS04 Bandwidth utilized: 20.965184 %

HP Network Team _1 on EURXMBS04 Bandwidth utilized: 10.7366888 % "