Good day, I´m relativly new to PowerShell, I come from C# and need to know something special.
How can I add the output of a pipeline into an existing array?
I do something like that example:
$FirstCollection = New-Object System.Collections.ArrayList
foreach ($item in $OtherCollection) {
if ($item -ne "blabla") {
$result |
Where-Object { ($_.ID -Match 1) } |
Group-Object -Property id, LevelDisplayName, LogName -NoElement |
Sort-Object -Property count -Descending |
**This output must going into the $FirstCollection**
}
if ($item -ne "bliblablubb") {
$result |
Where-Object { ($_.ID -Match 1) } |
Group-Object -Property id, LevelDisplayName, LogName -NoElement |
Sort-Object -Property count -Descending |
**This output must going into the $FirstCollection as well**
}
}
How the cow should I add the last output into an array. The array can also be a simple $array = @()
I need this, because after the foreach i have to output the array entries into a file. After the last pipe I get a Count, a value and a string and i need all outcomes line by line among themselves.
If do output directly after the pipline with Out-File...
I´m getting something like this:
Count Name
----- ----
69 123, someText, someMoreText
Count Name
----- ----
50 456, someText, someMoreText
Count Name
----- ----
25 789, someText, someMoreText
But I need:
Count Name
----- ----
69 123, someText, someMoreText
50 456, someText, someMoreText
25 789, someText, someMoreText
Can anybody help me to get this crap working, please?
Thanks a lot!!
CodePudding user response:
To add to an ArrayList in PowerShell, you do not pipe. Rather you use the ArrayList.Add()
function or simply =
(just like in C#).
$FirstCollection = New-Object System.Collections.ArrayList
foreach ($item in $OtherCollection) {
if ($item -ne "blabla") {
$FirstCollection = $result |
Where-Object { ($_.ID -Match 1) } |
Group-Object -Property id, LevelDisplayName, LogName -NoElement |
Sort-Object -Property count -Descending
}
if ($item -ne "bliblablubb") {
$FirstCollection = $result |
Where-Object { ($_.ID -Match 1) } |
Group-Object -Property id, LevelDisplayName, LogName -NoElement |
Sort-Object -Property count -Descending
}
}
I believe this also works with $FirstCollection = @()
.