Created
September 10, 2017 15:36
-
-
Save Youka/64fe93b3f59e802d59ea5c836352e71e to your computer and use it in GitHub Desktop.
Logs used network bandwidth for some time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Configuration | |
| $run_duration_seconds = 10 | |
| $read_delay_milliseconds = 500 | |
| $log_file = "log.txt" | |
| $verbose = $true | |
| # Network statistics | |
| $statistics = @{} | |
| $start_time = Get-Date | |
| $end_time = $start_time.addSeconds($run_duration_seconds) | |
| # Repeat for given duration | |
| while($end_time -gt (Get-Date)){ | |
| # Iterate through current available network interfaces | |
| foreach($net_interface in (Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkInterface)) { | |
| # Extract relevant interface information | |
| $name = $net_interface.name | |
| $bps = $net_interface.BytesTotalPersec * 8 | |
| $bps_max = $net_interface.CurrentBandwidth | |
| # Save or update statistics | |
| if(!$statistics.ContainsKey($name)){ | |
| $statistics.Add($name, @($bps, $bps_max)) | |
| } else { | |
| $item = $statistics.Get_Item($name) | |
| $item[0] += $bps | |
| $item[1] += $bps_max | |
| } | |
| # Output current activity if configured | |
| if($verbose -and $bps -gt 0) { | |
| Write-Host ("{0};{1};{2}/{3}" -f (Get-Date), $name, $bps, $bps_max) | |
| } | |
| } | |
| # Wait some time till next read | |
| Start-Sleep -milliseconds $read_delay_milliseconds | |
| } | |
| # Log statistics | |
| "Time: {0} - {1}" -f $start_time, $end_time | Out-File $log_file -Append | |
| $statistics | Format-Table @{Label="Interface"; Expression={$_.Name}}, @{Label="Coverage (in %)"; Expression={$_.Value[0] / $_.Value[1] * 100}} | Out-File $log_file -Append |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment