YAWPU

YAW what!? No, I’m not trying to learn some Iroquoian language. That’s just the acronym of the useless script I just wrote to demonstrate a friend how Write-Progress can be hijacked.
Thats why I called it YAWPU: stands for Yet Another Write-Progress Usage!
Here we will see how to monitor (and graph) network usage with Write-Progress :

$nic=(Get-WmiObject -Class Win32_NetworkAdapter | Where {($_.Netenabled) -and ($_.NetConnectionStatus -eq 2)} | Select -ExpandProperty Name Name) -replace "\(","[" -replace "\)","]"

While ($true) {
    $counters=Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkInterface | ? {$_.Name -eq "$nic"}
    $counters | ForEach-Object {
    $counterR=$_.BytesReceivedPersec
    $counterS=$_.BytesSentPersec
    $counterT=$_.BytesTotalPersec
    $nicSpeed=$_.CurrentBandwidth
    $totalPercent=[math]::Round(((($counterT*8)/$nicSpeed)*100),2)
    Write-Progress -Id 1 -Activity "Network Usage (Total)" -Status "Current: $counterT bytes per sec ($totalPercent %)" -PercentComplete $totalPercent
    Write-Progress -Id 2 -ParentId 1 -Activity "Network Usage (Bytes Received/sec)" -Status "Current: $counterR bytes received per sec" -PercentComplete ([math]::Round(((($counterR*8)/$nicSpeed)*100),2))
    Write-Progress -Id 3 -ParentId 1 -Activity "Network Usage (Bytes Sent/sec)" -Status "Current: $counterS bytes sent per sec" -PercentComplete ([math]::Round(((($counterS*8)/$nicSpeed)*100),2))
    Start-Sleep 1
    }
} #EndOf While ($true)

On friday, it’s all about having fun…

Leave a Reply

Scroll to Top