Foreach where with two arguments

Welcome Forums General PowerShell Q&A Foreach where with two arguments

Viewing 9 reply threads
  • Author
    Posts
    • #200330
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi people,

      Someone know how insert two arguments where at the end of the foreach below? If variable $_.Sinal is 0 (zero) the message is fiber cut, if $_.Sinal is 2 the message is Poweroff.

      
       $i=0
      
      $Sinalcli = ($column1,$column2)[1]|
      
      foreach {
      
        new-object psobject -property @{
      
                                          Sinal = $Column1[$i]
      
                                          Cliente = $column2[$i++]
      
                                         
      
                                         }
      
         
      
                                         
      
         
      
        } | where Sinal -EQ 0 | %{write-output "[ONU: $($_.Cliente) STATUS: SEM SINAL]"} | -EQ 2 | %{write-output "[ONU: $($_.Cliente) STATUS: Poweroff]"} |sort
      
          
      
       
      
      

       

      Many thanks for any help.

    • #200351
      Moderator
      Topics: 3
      Replies: 212
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Looks like your requirement is not exactly parsed.

      And what are coming from $column1 and $column2? And you can also try switch statement.

      Thank you.

    • #200408
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi Kiran,

      This script queries a device and returns two tables. I pass the variables the values and treat them in this foreach. However, there was a change in the way the equipment sends information adding an additional value. In this case what I need is if it is value = 1 the message is “Fiber cut” but if it is = 2 the message is “Poweroff”

      Thanks a lot for the help.

    • #200411
      js
      Participant
      Topics: 28
      Replies: 772
      Points: 2,220
      Helping Hand
      Rank: Community Hero

      Why not inside the loop say this.

      foreach { 
        if ($Column1[$i] -EQ 0) {
          write-output "[ONU: $($column2[$i++]) STATUS: SEM SINAL]"
        } elseif ($Column1[$i] -EQ 2) { 
          write-output "[ONU: $($column2[$i++]) STATUS: Poweroff]"
        } 
      }
      
      • This reply was modified 2 months ago by js.
      • This reply was modified 2 months ago by js.
      • This reply was modified 2 months ago by js.
    • #200414
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi Mr. JS,

       

      Thanks a lot for your reply. After put the code the following error appear:

       ForEach-Object : Cannot bind parameter ‘RemainingScripts’. Cannot convert the “if” value of type “System.String” to type “System.Management.Automation.ScriptBlock”.

      At line:65 char:1

      + foreach {

      + ~~~~~~~~~

          + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException

          + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

       

       

    • #200426
      js
      Participant
      Topics: 28
      Replies: 772
      Points: 2,220
      Helping Hand
      Rank: Community Hero

      I’m not sure what you’re doing. I made a correction to my answer. It’s good to learn the powershell basics.

    • #200432
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Thanks Js,

      I already change the code and the return is:

      
       <text>ONU STATUS:                                                                                                                                                      
      
                                                                                                                                                  [ONU: 11/12/7 STATUS: Power
      
       Off] [ONU: 11/14/3 STATUS: Power Off] [ONU: 11/16/1 STATUS: Power Off] [ONU: 11/16/3 STATUS: Power Off] [ONU: 11/5/11 STATUS: Power Off] [ONU: 11/6/5 STATUS: Power Of
      
      f] [ONU: 11/7/1 STATUS: Signal Off] [ONU: 11/9/10 STATUS: Signal Off] [ONU: 12/10/2 STATUS: Power Off] [ONU: 12/11/13 STATUS: Signal Off] [ONU: 12/16/3 STATUS: Power O
      
      ff] [ONU: 12/6/6 STATUS: Signal Off]</text> 
      
      

      It is almost OK. Only the space (tabulation) we need adjust.

       

      Thank you again.

    • #200441
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Showing mor parts of code:

      
      $Sinalcli = ($column1,$column2)[1]|
      
      foreach {
       new-object psobject -property @{
      
            Sinal = $Column1[$i]
      
            Cliente = $column2[$i++]
      
           }
      
           if ($Column1[$i] -EQ 0){
           write-output "[ONU: $($column2[$i++]) STATUS: Signal Off]"
        }
       elseif ($Column1[$i] -EQ 2) {
          write-output "[ONU: $($column2[$i++]) STATUS: Power Off]"
        }
      }|sort
       if ($TotalSinalCLi -ge 1){write-host "ONU STATUS: $($Sinalcli)" 
      

      And the result with |sort is:

      
      ONU STATUS:
      [ONU: 11/11/9 STATUS: Power
      
      Off] [ONU: 11/13/1 STATUS: Power Off] [ONU: 11/15/1 STATUS: Power Off] [ONU: 11/16/3 STATUS: Power Off] [ONU: 11/5/11 STATUS: Power Off] [ONU: 11/6/5 STATUS: Power Off
      
      ] [ONU: 11/7/1 STATUS: Signal Off] [ONU: 11/9/10 STATUS: Signal Off] [ONU: 12/10/3 STATUS: Power Off] [ONU: 12/16/3 STATUS: Power Off] [ONU: 12/6/6 STATUS: Signal Off]
      
      [ONU: 12/8/6 STATUS: Power Off]
      
      

       

      • This reply was modified 2 months ago by kvprasoon.
      • This reply was modified 2 months ago by kvprasoon. Reason: code formatting
      • This reply was modified 2 months ago by kvprasoon. Reason: code formatting
    • #200480
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi people,

       

      Thanks so much for all your time to reply that post. The code is working. The tabspace isn’t a problem. Now, I search a way to count how many times the number 0 and the number 2 appear int two other variables like: 0 = $signaoff and 2=$poweroff.

       

      Thanks.

    • #200507
      Participant
      Topics: 12
      Replies: 36
      Points: 32
      Rank: Member

      Hi people,

      Here the solution that works for me.

      
       $signaloff = @($strnum)
      
      $signaloff =   $signaloff -match '0'
      
      $poweroff = @($strnum)
      
      $poweroff =   $poweroff -match '2'
      
      $signaloff.Count
      
      $poweroff.count 
      
      

       

      Thank you all.

       

Viewing 9 reply threads
  • You must be logged in to reply to this topic.