Foreach where with two arguments

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.

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.

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.

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]"
  } 
}

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: (:slight_smile: [ForEach-Object], ParameterBindingException

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

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

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.

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]

 

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.

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.