# Foreach where with two arguments

**URL:** <https://forums.powershell.org/t/foreach-where-with-two-arguments/13720>\
**Category:** PowerShell Help\
**Created:** [January 27, 2020, 10:05pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720 "2020-01-27T22:05:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 27, 2020, 10:05pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/1 "2020-01-27T22:05:29Z")

</div>

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
```

&nbsp;

Many thanks for any help.

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [January 27, 2020, 11:27pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/2 "2020-01-27T23:27:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 7:37am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/3 "2020-01-28T07:37:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [January 28, 2020, 8:11am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/4 "2020-01-28T08:11:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 8:21am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/5 "2020-01-28T08:21:20Z")

</div>

Hi Mr. JS,

&nbsp;

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 {

- 

```auto

```

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

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

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [January 28, 2020, 8:38am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/6 "2020-01-28T08:38:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 8:53am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/7 "2020-01-28T08:53:07Z")

</div>

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.

&nbsp;

Thank you again.

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 9:22am UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/8 "2020-01-28T09:22:02Z")

</div>

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]
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 12:27pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/9 "2020-01-28T12:27:13Z")

</div>

Hi people,

&nbsp;

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.

&nbsp;

Thanks.

---

<div class="post-metadata">

**Author:** ![rodrigocorte](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rodrigocorte](https://forums.powershell.org/u/rodrigocorte)\
**Post date:** [January 28, 2020, 3:33pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/10 "2020-01-28T15:33:09Z")

</div>

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
```

&nbsp;

Thank you all.

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/foreach-where-with-two-arguments/13720/11 "2024-05-16T20:30:46Z")

</div>


