I’ve been pulling my hair out at this one for the last few hours, seems to be down to an obscure quirk.
(EDITED)
Import this function:
Function Test-Filter
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true,
ValueFromPipeline,
Position = 0)]
$Items
)
Process
{
$Items | Foreach-Object {
if ($_ -eq "A")
{
$output += $_
}
}
}
End
{
if ($output.Count -gt 0)
{ Write-Output (,$output) }
}
}
Now run this:
[Array]$arr1 = "A", "B", "C", "D" | Test-Filter | Sort [Array]$arr2 = "E", "F", "G", "H" | Test-Filter | Sort | Select -First 1 [Array]$arr3 = 1, 2 | Test-Filter [Array]$arr4 = "Jolly Cooperation" | Test-Filter [Array]$combined = $arr1 + $arr2 + $arr3 + $arr4 $combined.count
Count is 1, as expected, $arr1 contains “A” and the rest are empty. Now run this:
Invoke-Command {
[Array]$arr1 = "A", "B", "C", "D" | Test-Filter | Sort
[Array]$arr2 = "E", "F", "G", "H" | Test-Filter | Sort | Select -First 1
[Array]$arr3 = 1, 2 | Test-Filter
[Array]$arr4 = "Jolly Cooperation" | Test-Filter
[Array]$combined = $arr1 + $arr2 + $arr3 + $arr4
$combined.count
}
Count is now 4.
This is part of a process to list patch files awaiting installation, there are four types of files but not all may be present in each patch type. Invoke-Command is required to format and log the output of the (lengthy) script but it seems to be messing with arrays somehow?