0

I have been working on a script that has an interactive cli menu to select from. I've got everything working and the script itself works but for some reason the selection menu (formatted as an ArrayList object) won't display during run when called, rather it displays at the end of script. What am I missing?

[System.Collections.ArrayList] $menuarray = @() # init blank array

$somelist1 = Get-SomeDataHere | ConvertFrom-Json

$init = 0

while ($init -ne $somelist1.items.Count) 
{

  $menuarray.add([PSCustomObject]@{Number = $init;somenamedthing = $somelist1.items.metadata.name[$init];someaddress = $somelist1.items.status.someaddress[$init]}) > $null

  $init = $init+1

}

write-host "Printing Array"

$menuarray

$menuselection = -1

While($menuselection -notin 0..$somelist1.items.count) # simple while loop to require the user to enter a selection from the list.

{

  write-host "Specify number of items you would like to interact with:"

  $menuselection = Read-host

}

write-host "You picked $menuselection with associated address of:" $somelist1.items.status.someaddress[$menuselection]

I've tried running the script section outside (directly in powershell so I can see what is happening), tried calling the $menuarray in different areas, with echo, without echo, etc.

aterrell4
  • 1
  • 1
  • In short, when `Format-Table` is called implicitly by the `$menuarray` line, output is delayed for a short duration so `Format-Table` can decide on "optimal" column width. To solve the problem, change that line to an explicit `Format-Table` call: `$menuarray | Format-Table`. – zett42 Dec 29 '22 at 20:21
  • 1
    Thanks @zett42 ! That fixed it and the recommended solution [found in this post](https://stackoverflow.com/questions/43689289/powershell-output-is-crossing-between-functions) was exactly the behavior I was experiencing. Didn't know what was happening or how to describe it so the explanation there was extremely helpful. – aterrell4 Dec 29 '22 at 20:34

0 Answers0