forked from sperner/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetTraffic.ps1
74 lines (64 loc) · 2.48 KB
/
NetTraffic.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Simple wrapper to measure network traffic of a given command
# (diff sent/recv bytes before and after invokation)
#
# no args given -> print usage
if( $($args.count) -lt 1 )
{
Write-Host "usage: $($MyInvocation.MYCommand) <command> [<param1>...<paramN>]"
exit -1
}
# combine args to command
for( $index=0; $index -lt $($args.count); $index++ )
{
$command += $args[$index]
$command += " "
}
# get old network statistics
$computer = "LocalHost"
$namespace = "root\CIMV2"
$NICdatas = Get-WmiObject -class Win32_PerfRawData_Tcpip_NetworkInterface -computername $computer -namespace $namespace
$names = (0..($NICdatas.Length-1))
$oldReceived = (0..($NICdatas.Length-1))
$oldSent = (0..($NICdatas.Length-1))
$oldTotal = (0..($NICdatas.Length-1))
$newReceived = (0..($NICdatas.Length-1))
$newSsent = (0..($NICdatas.Length-1))
$newTotal = (0..($NICdatas.Length-1))
$diffReceived = (0..($NICdatas.Length-1))
$diffSsent = (0..($NICdatas.Length-1))
$diffTotal = (0..($NICdatas.Length-1))
# save old network statistics
$aktNum = 0
ForEach( $NicData IN $NICdatas )
{
$names[$aktNum] = $NicData | Select Name
$oldReceived[$aktNum] = $NicData | Select BytesReceivedPersec
$oldSent[$aktNum] = $NicData | Select BytesSentPersec
$oldTotal[$aktNum] = $NicData | Select BytesTotalPersec
$aktNum++
}
# invoke command
Invoke-Expression $command
# get new network statistics
$NICdatas = Get-WmiObject -class Win32_PerfRawData_Tcpip_NetworkInterface -computername $computer -namespace $namespace
# save new network statistics
$aktNum = 0
ForEach( $NicData IN $NICdatas )
{
$newReceived[$aktNum] = $NicData | Select BytesReceivedPersec
$newSsent[$aktNum] = $NicData | Select BytesSentPersec
$newTotal[$aktNum] = $NicData | Select BytesTotalPersec
$aktNum++
}
# calculate the differences beteen old and new
For( $index = 0; $index -lt $NICdatas.Length; $index++ )
{
$diffReceived[$index] = [int]$newReceived[$index].BytesReceivedPersec - [int]$oldReceived[$index].BytesReceivedPersec
$diffSsent[$index] = [int]$newSsent[$index].BytesSentPersec - [int]$oldSent[$index].BytesSentPersec
$diffTotal[$index] = [int]$newTotal[$index].BytesTotalPersec - [int]$oldTotal[$index].BytesTotalPersec
}
# print the differences
For( $index = 0; $index -lt $NICdatas.Length; $index++ )
{
Write-Host "`n$($names[$index].Name)`n`rRevceived:`t$("{0:N0}" -f $($diffReceived[$index]))`tSent:`t$("{0:N0}" -f $($diffSsent[$index]))`tTotal:`t$("{0:N0}" -f $($diffTotal[$index]))"
}