Reference Elements in PowerShell Multidimensional Array -
i have csv file

my powershell script attempts store sourceip, destinationip, , traffic in multidimensional array
$source = @((import-csv d:\script\my.csv).sourceip) $dest = @((import-csv d:\script\my.csv).destinationip) $t = @((import-csv d:\script\my.csv).traffic) $multi = @($source),@($dest),@($t) when try read first element of $multi, expect list of sourceip
foreach ($q in $multi){ write-host $q[0] write-host `n } but instead, sourceip, destinationip, traffic, i.e.
10.153.128.110 10.251.68.80 3.66 gb and if try
foreach ($q in $multi){ write-host $q[0][0][0] write-host `n } i
1 1 3 how troubleshoot?
update
ultimate goal to
- count total traffic
- count traffic if sourceip or destination ip fits pattern, i.e. 10.251.22.x
- get percentage
update ii
i able code import csv , tally total bandwidth only, need bandwidth sourceip , destinationip pattern.
$t = @((import-csv d:\script\my.csv).traffic) foreach ($k in $t){ write-host $k } foreach ($i in $t){ $j += ,@($i.split(" ")) } foreach ($m in $j){ switch ($m[1]){ gb { $m[0] = [int]($m[0]) * 1000 $m[1] = 'mb' } mb {} kb { $m[0] = [int]($m[0]) / 1000 $m[1] = 'mb' } } $total_bandwidth += $m[0] } write-host total bandwidth ("{0:n2}" -f $total_bandwidth) mb
you should not split array of object multiple parallel arrays of properties. easy operate when objects whole.
$scale=@{ b=1e00 kb=1e03 mb=1e06 gb=1e09 tb=1e12 } $trafficbytes={ $a=-split$_.traffic [double]$a[0]*$scale[$a[1]] } import-csv d:\script\my.csv| foreach-object $trafficbytes| measure-object -sum #total traffic import-csv d:\script\my.csv| where-object {$_.destinationip-like'10.*'}| #condition foreach-object $trafficbytes| measure-object -sum #traffic condition
Comments
Post a Comment