0

I have CSV file

enter image description here

My PowerShell Script attempts to store SourceIP, DestinationIP, and 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 I try to read from first element of $multi, I expect to get a list of SourceIP

foreach ($q in $multi){
    write-host $q[0]
    write-host `n
}

But instead, I get SourceIP, DestinationIP, Traffic, i.e.

10.153.128.110


10.251.68.80


3.66 GB

And if I try

foreach ($q in $multi){
    write-host $q[0][0][0]
    write-host `n

}

I get

1


1


3

How to troubleshoot?

UPDATE

Ultimate goal is to

  1. Count total traffic
  2. Count traffic if SourceIP or Destination IP fits into certain pattern, i.e. 10.251.22.x
  3. Get percentage

UPDATE II

I am able to get code to import CSV and tally total bandwidth only, but I also need bandwidth from SourceIP and DestinationIP with certain 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 is ("{0:N2}" -f $total_bandwidth) MB 
7
  • Sorry but I don't understand: what are you trying to get ? Commented May 11, 2015 at 13:32
  • @CB How to reference a list of SourceIP from the $multi array Commented May 11, 2015 at 13:34
  • you mean this: multi[0] ? Commented May 11, 2015 at 13:45
  • What is the ultimate goal? The data is already structured. Why exactly do you feel that you need to put it into another structure? Commented May 11, 2015 at 14:02
  • 1
    @Sonihal cast your IP [string] to [ipaddress] and compare it $ip.sortableaddress from the limits you want and go on... Commented May 11, 2015 at 14:46

2 Answers 2

2

You should not split array of object to multiple parallel arrays of properties. It is much easy to operate when objects are 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 by condition
Sign up to request clarification or add additional context in comments.

Comments

1

PetSerAl has a good idea for the conversion, but here is a way to do this that requires iterating the CSV only once and will give your percentages.

$filter = "10.251.22.*"

$Scale=@{
     B=1e00
    KB=1e03
    MB=1e06
    GB=1e09
    TB=1e12
}


$myCsv = Import-Csv D:\Script\my.csv | Select-Object *, @{ Name = "TrafficBytes"; Expression = { $a = -split $_.Traffic; [double] $a[0] * $Scale[$a[1]] } }
$trafficFiltered = $myCsv | Group-Object { $_.SourceIP -like $filter -or $_.DestinationIP -like $filter } | Select-Object @{ Name = "IPFilter"; Expression = { if ($_.Name -eq $true) { $filter } else { "Other" } } }, @{ Name = "TrafficBytes"; Expression = { ($_.Group | Measure-Object -Sum "TrafficBytes").Sum } }
$trafficTotal = $myCsv | Measure-Object -Sum TrafficBytes
$trafficReport = Select-Object IPFilter, TrafficBytes, @{ Name = "Percent"; Expression = { "{0:P}" -f $_.TrafficBytes / $trafficTotal.Sum * 100.0 } }

$trafficReport

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.