Working with tables

Welcome Forums General PowerShell Q&A Working with tables

This topic contains 17 replies, has 7 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #83468

    Participant
    Points: 1
    Rank: Member

    Hi,

    I have a powershell script that create a table with two columns. I would like use a conditional ( if ) and put de values searched into a news variables. See below:
    SinaldBm Cliente
    ——– ——-
    -19 16777472
    -20 16777728
    -19 16777984
    -19 16778240
    0.0 16778496
    -19 16778752
    -23 16779008
    -18 16779264
    -19 16779520
    -22 16779776
    -27 16780032
    -18 16780288
    -19 16780544
    -19 16780800
    -21 16781056
    -24 16781568
    -19 16781824
    -22 16782080
    -21 16782336
    -21 16782592
    0.0 16782848
    -22 16783104
    -23 16783360
    0.0 16783616
    -22 16783872
    -24 16784128
    -24 16784384
    -26 16784640
    -22 16784896
    -22 16785152
    -23 16785408
    -21 16785664
    -25 16785920

    I Need to show only combined data with value less or equal to -25. Ex:
    -26 16784640
    -25 16785920

    Thank you for advance...

    rc.

  • #83473

    Participant
    Points: 22
    Rank: Member

    Hello.

    If the output of your script is object, then:

    your_script | where SinaldBm -le -25
    
    • #83474

      Participant
      Points: 1
      Rank: Member

      Hi,

      My code is like below ( a part of)

      $column1 = @($strcli)
      $column2 = @($strnum)

      $i=0
      ($column1,$column2 | sort length)[1] |
      foreach {
      new-object psobject -property @{
      Cliente = $Column1[$i]
      SinaldBm = $column2[$i++]
      }
      } | ft -auto

      Where I must put your suggest?

      tks.

    • #83477

      Participant
      Points: 22
      Rank: Member

      You should put it before "ft -auto".

  • #83483

    Participant
    Points: 0
    Rank: Member

    If you were reading it from a file, you could use import-csv:

    $filename = ".\SignaldBm.txt"
    $alllines = Import-Csv $filename -Delimiter ' '  | where SinaldBm -le 25
    $alllines
    
    • #83510

      Participant
      Points: 1
      Rank: Member

      Thanks for your reply,

      Actually, the script run a snmpwalk to a equipment. I need to show, after put the data in a table(s) only value minor that -25...or another after parametrization.

  • #83528

    Participant
    Points: 57
    Rank: Member

    Rules to remember in PowerShell. Filter as far to the left as possible and format to the right. Once you format your data you are done.

  • #83594

    Participant
    Points: 1
    Rank: Member

    Hi all,

    With Where don't work. I tried with IF command but don't work too.

    Below all script:

    $target = @()
    $ini| foreach {
        
        $strnum = (snmpwalk.exe -Ln -On -v 2c -c public $hostip ".1.3.6.1.4.1.3709.3.6.2.1.1.22")
        
        $strnum.Count
        $strcli = $strnum
    
    
        ##Remove String Chars from Uptime###
        $strNum = $strnum.Substring(51,3)
        $strcli = $strcli.Substring(31,9)
        
       
     
    }
       
       $strnum
       $strcli
        
        #write-host "Total Clientes:$($strnum.count)Clientes Limite: $($strnum |sort)"
      
    $column1 = @($strnum)
    $column2 = @($strcli)
    
    $i=0
    ($column1,$column2 | sort length)[1]|
    foreach {
      new-object psobject -property @{
                                        Sinal = $Column1[$i]
    
                                        Cliente = $column2[$i++]
                                       
                                       }
       
                                       
       
        
        where $column1[$i] -le -28
      }  | ft -AutoSize
    
      
  • #83833

    Participant
    Points: 1
    Rank: Member

    Hi people,

    My script don't work yet. Someone has an idea how I could do this?

    Have another doubt: How I get the value of the first column? EX:
    POP1 -25
    POP2 -27
    POP3 -25
    POP4 -28
    POP5 -24
    POP6 -23
    POP7 -24
    POP8 -23

    I need get this: POP4 -28

    Thank you.

    rc.

  • #83836

    Participant
    Points: 1
    Rank: Member

    Remember: POP4 is in a array, -28 is in another one.

  • #83851

    Participant
    Points: 0
    Rank: Member

    Hey Rodrigo,
    Like Sergey said, it should be before the ft -auto

    Currently you have:

      }  | ft -AutoSize

    It would go:

      }  | ‹here› | ft -AutoSize
  • #83923

    Participant
    Points: 1
    Rank: Member

    Hi people,

    Thank you so much for the all help! Now, the code is working. Is possible to remove the first line of the result?

    Result of script:
    Sinal Cliente
    —– ——-
    -18 16777472
    -20 16777728
    -19 16777984
    -19 16778240
    0.0 16778496
    -19 16778752
    -24 16779008
    -17 16779264
    -19 16779520
    -22 16779776
    0.0 16780032
    -18 16780288
    -19 16780544
    -19 16780800
    -21 16781056
    -25 16781568
    -19 16781824
    -22 16782080
    -21 16782336
    -21 16782592
    -22 16783104
    -22 16783360
    -22 16783616
    -22 16783872
    -24 16784128
    -24 16784384
    0.0 16784640
    0.0 16784896
    -22 16785152
    -23 16785408
    -21 16785664
    -25 16785920
    -22 16786176
    -24 16786432
    0.0 16786688
    -25 16786944
    -30 16787200
    -22 16787456
    -24 16787712
    -25 16787968
    0.0 16788224
    -22 16788480
    -24 16788736
    -24 16788992
    -22 16789248
    -21 16789504
    -24 16789760
    -26 16789762
    -22 16790016
    0.0 16790272
    -22 16790528
    -23 16790784
    -22 16791040
    -22 16791296
    -21 16791552
    0.0 16791808

    Result After applied the filter
    Sinal Cliente
    —– ——-
    -25 16781568
    -25 16785920
    -25 16786944
    -30 16787200
    -25 16787968
    -26 16789762

    Result expected for my monitoring system
    -25 16781568
    -25 16785920
    -25 16786944
    -30 16787200
    -25 16787968
    -26 16789762

  • #83929
    Ron

    Participant
    Points: 0
    Rank: Member

    Instead of

    | ft -AutoSize

    Use

    | %{write-output "$($_.Sinal) $($_.Cliente)"}
  • #83936

    Participant
    Points: 1
    Rank: Member

    Thank you all! The script works fine!!!

    rc.

  • #108403

    Participant
    Points: 1
    Rank: Member

    Hello people, I hop you are fine!!

     

    There is another challenge, well, for me, because for you is very easy.. 🙂

     

    Here a code that I need.

    $i=0
    $PortStatus = ($column1,$column2,$column3,$column4)[3]|
    foreach {
      new-object psobject -property @{
                                        TX_Signal = $Column1[$i]
                                        RX_Signal = $column2[$i]
                                        Temperature = $column3[$i]
                                        Port_ID = $column4[$i++]
                                       
                                       
                                       }
       
                                       
      
    }#|where $_.TX_Signal -ge 0 | %{write-output "[$($_.Port_ID)]"} | sort
    
    write-host ""
    $x = 0
    Foreach ($d in ($PortStatus.Port_ID)){Foreach ($c in ($PortStatus.RX_Signal)){
    
    
    write-host ""
    write-host "RX Signal ETH-$d"
    write-host "$($c)"
    Write-host "1"
    write-host ""
    $x++
    }}
    

    The result is:

    RX Signal ETH-1
    -6.6
    1

    RX Signal ETH-1
    -10.3
    1

    RX Signal ETH-1
    -7.1
    1

    RX Signal ETH-1
    -8.0
    1

    RX Signal ETH-1
    -12.9
    1

    RX Signal ETH-1
    -8.8
    1

    RX Signal ETH-1
    -8.3
    1

    RX Signal ETH-1
    -9.7
    1

    RX Signal ETH-1
    -9.5
    1

    RX Signal ETH-1
    -9.1
    1

    RX Signal ETH-1
    -6.1
    1

    RX Signal ETH-1
    -2.1
    1

    RX Signal ETH-1
    -4.0
    1

    RX Signal ETH-2
    -6.6
    1

    RX Signal ETH-2
    -10.3
    1

    RX Signal ETH-2
    -7.1
    1

    RX Signal ETH-2
    -8.0
    1

    RX Signal ETH-2
    -12.9
    1

    RX Signal ETH-2
    -8.8
    1

    RX Signal ETH-2
    -8.3
    1

    RX Signal ETH-2
    -9.7
    1

    RX Signal ETH-2
    -9.5
    1

    RX Signal ETH-2
    -9.1
    1

    RX Signal ETH-2
    -6.1
    1

    RX Signal ETH-2
    -2.1
    1

    RX Signal ETH-2
    -4.0
    1

    RX Signal ETH-3
    -6.6
    1

    RX Signal ETH-3
    -10.3
    1

    RX Signal ETH-3
    -7.1
    1

    RX Signal ETH-3
    -8.0
    1

    RX Signal ETH-3
    -12.9
    1

    RX Signal ETH-3
    -8.8
    1

    RX Signal ETH-3
    -8.3
    1

    RX Signal ETH-3
    -9.7
    1

    RX Signal ETH-3
    -9.5
    1

    RX Signal ETH-3
    -9.1
    1

    RX Signal ETH-3
    -6.1
    1

    RX Signal ETH-3
    -2.1
    1

    RX Signal ETH-3
    -4.0
    1

    RX Signal ETH-4
    -6.6
    1

    RX Signal ETH-4
    -10.3
    1

    RX Signal ETH-4
    -7.1
    1

    RX Signal ETH-4
    -8.0
    1

    RX Signal ETH-4
    -12.9
    1

    RX Signal ETH-4
    -8.8
    1

    RX Signal ETH-4
    -8.3
    1

    RX Signal ETH-4
    -9.7
    1

    RX Signal ETH-4
    -9.5
    1

    RX Signal ETH-4
    -9.1
    1

    RX Signal ETH-4
    -6.1
    1

    RX Signal ETH-4
    -2.1
    1

    RX Signal ETH-4
    -4.0
    1

    RX Signal ETH-5
    -6.6
    1

    RX Signal ETH-5
    -10.3
    1

    RX Signal ETH-5
    -7.1
    1

    RX Signal ETH-5
    -8.0
    1

    RX Signal ETH-5
    -12.9
    1

    RX Signal ETH-5
    -8.8
    1

    RX Signal ETH-5
    -8.3
    1

    RX Signal ETH-5
    -9.7
    1

    RX Signal ETH-5
    -9.5
    1

    RX Signal ETH-5
    -9.1
    1

    RX Signal ETH-5
    -6.1
    1

    RX Signal ETH-5
    -2.1
    1

    RX Signal ETH-5
    -4.0
    1

    RX Signal ETH-6
    -6.6
    1

    RX Signal ETH-6
    -10.3
    1

    RX Signal ETH-6
    -7.1
    1

    RX Signal ETH-6
    -8.0
    1

    RX Signal ETH-6
    -12.9
    1

    RX Signal ETH-6
    -8.8
    1

    RX Signal ETH-6
    -8.3
    1

    RX Signal ETH-6
    -9.7
    1

    RX Signal ETH-6
    -9.5
    1

    RX Signal ETH-6
    -9.1
    1

    RX Signal ETH-6
    -6.1
    1

    RX Signal ETH-6
    -2.1
    1

    RX Signal ETH-6
    -4.0
    1

    RX Signal ETH-8
    -6.6
    1

    RX Signal ETH-8
    -10.3
    1

    RX Signal ETH-8
    -7.1
    1

    RX Signal ETH-8
    -8.0
    1

    RX Signal ETH-8
    -12.9
    1

    RX Signal ETH-8
    -8.8
    1

    RX Signal ETH-8
    -8.3
    1

    RX Signal ETH-8
    -9.7
    1

    RX Signal ETH-8
    -9.5
    1

    RX Signal ETH-8
    -9.1
    1

    RX Signal ETH-8
    -6.1
    1

    RX Signal ETH-8
    -2.1
    1

    RX Signal ETH-8
    -4.0
    1

    RX Signal ETH-9
    -6.6
    1

    RX Signal ETH-9
    -10.3
    1

    RX Signal ETH-9
    -7.1
    1

    RX Signal ETH-9
    -8.0
    1

    RX Signal ETH-9
    -12.9
    1

    RX Signal ETH-9
    -8.8
    1

    RX Signal ETH-9
    -8.3
    1

    RX Signal ETH-9
    -9.7
    1

    RX Signal ETH-9
    -9.5
    1

    RX Signal ETH-9
    -9.1
    1

    RX Signal ETH-9
    -6.1
    1

    RX Signal ETH-9
    -2.1
    1

    RX Signal ETH-9
    -4.0
    1

    RX Signal ETH-10
    -6.6
    1

    RX Signal ETH-10
    -10.3
    1

    RX Signal ETH-10
    -7.1
    1

    RX Signal ETH-10
    -8.0
    1

    RX Signal ETH-10
    -12.9
    1

    RX Signal ETH-10
    -8.8
    1

    RX Signal ETH-10
    -8.3
    1

    RX Signal ETH-10
    -9.7
    1

    RX Signal ETH-10
    -9.5
    1

    RX Signal ETH-10
    -9.1
    1

    RX Signal ETH-10
    -6.1
    1

    RX Signal ETH-10
    -2.1
    1

    RX Signal ETH-10
    -4.0
    1

    RX Signal ETH-11
    -6.6
    1

    RX Signal ETH-11
    -10.3
    1

    RX Signal ETH-11
    -7.1
    1

    RX Signal ETH-11
    -8.0
    1

    RX Signal ETH-11
    -12.9
    1

    RX Signal ETH-11
    -8.8
    1

    RX Signal ETH-11
    -8.3
    1

    RX Signal ETH-11
    -9.7
    1

    RX Signal ETH-11
    -9.5
    1

    RX Signal ETH-11
    -9.1
    1

    RX Signal ETH-11
    -6.1
    1

    RX Signal ETH-11
    -2.1
    1

    RX Signal ETH-11
    -4.0
    1

    RX Signal ETH-21
    -6.6
    1

    RX Signal ETH-21
    -10.3
    1

    RX Signal ETH-21
    -7.1
    1

    RX Signal ETH-21
    -8.0
    1

    RX Signal ETH-21
    -12.9
    1

    RX Signal ETH-21
    -8.8
    1

    RX Signal ETH-21
    -8.3
    1

    RX Signal ETH-21
    -9.7
    1

    RX Signal ETH-21
    -9.5
    1

    RX Signal ETH-21
    -9.1
    1

    RX Signal ETH-21
    -6.1
    1

    RX Signal ETH-21
    -2.1
    1

    RX Signal ETH-21
    -4.0
    1

    RX Signal ETH-25
    -6.6
    1

    RX Signal ETH-25
    -10.3
    1

    RX Signal ETH-25
    -7.1
    1

    RX Signal ETH-25
    -8.0
    1

    RX Signal ETH-25
    -12.9
    1

    RX Signal ETH-25
    -8.8
    1

    RX Signal ETH-25
    -8.3
    1

    RX Signal ETH-25
    -9.7
    1

    RX Signal ETH-25
    -9.5
    1

    RX Signal ETH-25
    -9.1
    1

    RX Signal ETH-25
    -6.1
    1

    RX Signal ETH-25
    -2.1
    1

    RX Signal ETH-25
    -4.0
    1

    RX Signal ETH-26
    -6.6
    1

    RX Signal ETH-26
    -10.3
    1

    RX Signal ETH-26
    -7.1
    1

    RX Signal ETH-26
    -8.0
    1

    RX Signal ETH-26
    -12.9
    1

    RX Signal ETH-26
    -8.8
    1

    RX Signal ETH-26
    -8.3
    1

    RX Signal ETH-26
    -9.7
    1

    RX Signal ETH-26
    -9.5
    1

    RX Signal ETH-26
    -9.1
    1

    RX Signal ETH-26
    -6.1
    1

    RX Signal ETH-26
    -2.1
    1

    RX Signal ETH-26
    -4.0
    1

    The foreach is running one time for each Port_ID getting a Information for RX_Signal for every loop. I Need that loop run a time once putting the Port_ID and RX_Signal.

  • #108406

    Participant
    Points: 1
    Rank: Member

    Hello people, here me again....

    See the code below:

    $i=0
    $PortStatus = ($column1,$column2,$column3,$column4)[3]|
    foreach {
      new-object psobject -property @{
                                        TX_Signal = $Column1[$i]
                                        RX_Signal = $column2[$i]
                                        Temperature = $column3[$i]
                                        Port_ID = $column4[$i++]
                                       
                                       
                                       }
       
                                       
      
    }
    
    $x = 0
    Foreach ($d in ($PortStatus.Port_ID)){Foreach ($c in ($PortStatus.RX_Signal)){
    
    write-host ""
    write-host "RX Signal ETH-$d"
    write-host "$($c)"
    Write-host "1"
    write-host ""
    $x++
    }}
    

    The foreach is running one time for Port_ID for each data in RX_Signal. I need that run Only one time for RX_Signal and Port_ID.

    That currently script, the result is like below:

    RX Signal ETH-21
    -6.6
    1

    RX Signal ETH-21
    -10.3
    1

    RX Signal ETH-21
    -7.1
    1

    RX Signal ETH-21
    -8.0
    1

    RX Signal ETH-21
    -12.9
    1

    RX Signal ETH-21
    -8.8
    1

    RX Signal ETH-21
    -8.3
    1

    RX Signal ETH-21
    -9.7
    1

    RX Signal ETH-21
    -9.5
    1

    RX Signal ETH-21
    -9.1
    1

    RX Signal ETH-21
    -6.1
    1

    RX Signal ETH-21
    -2.1
    1

    RX Signal ETH-21
    -4.0
    1

    RX Signal ETH-25
    -6.6
    1

    RX Signal ETH-25
    -10.3
    1

    RX Signal ETH-25
    -7.1
    1

    RX Signal ETH-25
    -8.0
    1

    RX Signal ETH-25
    -12.9
    1

    RX Signal ETH-25
    -8.8
    1

    RX Signal ETH-25
    -8.3
    1

    RX Signal ETH-25
    -9.7
    1

    RX Signal ETH-25
    -9.5
    1

    RX Signal ETH-25
    -9.1
    1

    RX Signal ETH-25
    -6.1
    1

    RX Signal ETH-25
    -2.1
    1

    RX Signal ETH-25
    -4.0
    1

    RX Signal ETH-26
    -6.6
    1

    RX Signal ETH-26
    -10.3
    1

    RX Signal ETH-26
    -7.1
    1

    RX Signal ETH-26
    -8.0
    1

    RX Signal ETH-26
    -12.9
    1

    RX Signal ETH-26
    -8.8
    1

    RX Signal ETH-26
    -8.3
    1

    RX Signal ETH-26
    -9.7
    1

    RX Signal ETH-26
    -9.5
    1

    RX Signal ETH-26
    -9.1
    1

    RX Signal ETH-26
    -6.1
    1

    RX Signal ETH-26
    -2.1
    1

    RX Signal ETH-26
    -4.0
    1

  • #108520
    js

    Participant
    Points: 241
    Helping Hand
    Rank: Participant

    Hi, shouldn't SinaldBM be int?

    import-csv example.csv | where { [int]$_.sinaldbm -le -25 }
  • #108545

    Participant
    Points: 1
    Rank: Member

    Hi Js, Thank you for your reply,

    That issue was resolved. The next challenge is run foreach into another one.

    rc.

The topic ‘Working with tables’ is closed to new replies.