2

I have a function:

function Find-FooBars
{
  [CmdletBinding()]
  [OutputType([array])]
  param
  (
    [string]$appname,
    [string]$eventsId
  )
  $machines = @("server1", "server2")
  $rx = -join ($eventsId, "_.*\.json")
  $answer = [System.Collections.ArrayList]@()
  foreach ($machine in $machines)
  {
    $dir = -join ("\\", $machine, "\c$\Applications\", $appname, "\logs\alerts")
    $arr = Get-ChildItem $dir -Recurse |
      Where-Object { $_.Name -match $rx } |
      Select-Object @{ n = 'LastWriteTime'; e = { $_.LastWriteTime } }, @{ n = 'FullName'; e = { $_.FullName } }
    $answer.Add($arr)
  }
  $answer | Sort-Object -Property LastWriteTime
}

When I invoke the function:

Find-FooBars FooBarMaker 123456

This is the output:

0
1
    
LastWriteTime       FullName
-------------       --------
11/14/2021 10:30:09 \\server2\c$\Applications\FooBarMaker\logs\alerts\123456_2021-11-14-153008Z_New_1636903809040.json
11/14/2021 21:00:53 \\server2\c$\Applications\FooBarMaker\logs\alerts\123456_2021-11-15-020018Z_Update_1636941653674.json
11/15/2021 10:30:09 \\server2\c$\Applications\FooBarMaker\logs\alerts\123456_2021-11-15-153008Z_Update_1636990209129.json
11/15/2021 20:20:08 \\server1\c$\Applications\FooBarMaker\logs\alerts\123456_2021-11-16-012008Z_Update_1637025608823.json
11/15/2021 23:50:08 \\server1\c$\Applications\FooBarMaker\logs\alerts\123456_2021-11-16-045007Z_Update_1637038208704.json

I would like to get rid of the 0 and 1 at the beginning the output.

[ Edit: There were 2 other questions identified as possible duplicates that talked about indices being inserted into an Array, but I did not see extra values in my ArrayList. My array held just what I expected. But I did see what seemed like spurious extra Console output I wanted gone. ]

gknauth
  • 2,310
  • 2
  • 29
  • 44
  • 3
    `$null = $answer.Add($arr)` – zett42 Nov 17 '21 at 07:25
  • @zett42 That worked, thanks! – gknauth Nov 17 '21 at 07:26
  • @iRon @zett42 Those other answers answer a slightly different question, and are helpful in understanding the strange behavior I observed, but @zett42's first comment (to add the `$null =`) specifically answered my question. Another comment on the other question mentioned an `outNull` option to the `Add` method that might have also solved the problem. – gknauth Nov 17 '21 at 09:47
  • 1
    As for your latest edit: just look up what the `.Add()` method of the [ArrayList Class](https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist.add?view=net-5.0#returns) returns. – Theo Nov 17 '21 at 12:35

0 Answers0