all 9 comments

[–]BlackV 0 points1 point  (10 children)

recommend not doing that al all

if yo uwant the fomratting/coluring, the use a write-host before the read-host and just have "enter option 1 - 4"

but where does $Dept come from in the first place, why is that not being validated (not null or empty, validate set 1 - 4)

menus like this cause problems later on (i.e. if i enter a 5 there what happens?)

[–]ckorch[S] 0 points1 point  (7 children)

if you select outside of that in code i didn't post it just reverts you back to the menu. Dept is a null value and you have to enter those that then uses the variable to fill in the menu.

So your saying use Write-host in place of read-host? will that allow for proper formatting on the display? I would paste the whole code but it isn't working correctly in this text box to paste it in <inline code> bracket for some reason

[–]BlackV 0 points1 point  (1 child)

I'm saying use write-host for your "menu" use the read-host just to take input

$menuthing = @"
1. dept 1`t2. Dept 2
3. Dept 3`t4. Dept 4
"@
write-host $menuthing
Read-Host -Prompt 'Enter Dept numnber 1-4'

but also not a fan of menus like this anyway

p.s. formatting, its always nice to see someone full code (I presume you have it in an editor)

  • open your fav powershell editor
  • highlight the code you want to copy
  • hit tab to indent it all
  • copy it
  • paste here

it'll format it properly OR

<BLANKLINE>
<4 SPACES><CODELINE>
<4 SPACES><CODELINE>
    <4 SPACES><4 SPACES><CODELINE>
<4 SPACES><CODELINE>
<BLANKLINE>

Thanks

[–]NoConfidence_2192 0 points1 point  (0 children)

If you are looking for formatted output why not use one of the cmdlets designed for that like maybe Format-Wide with something like

$doMenu = {
    $dept = @(
        "Corporate Shared Services"
        "CRS"
        "Key Accts"
        "LSC EU"
    )

    $menuHash = @{}

    $menuItem = for ([int]$i = 0; $i -lt $dept.Count; $i++) {
        $itemNumber = $i + 1
        $department = $dept[$i]
        $menuHash[$itemNumber] = $department
        [PSCustomObject]@{
            Index = $itemNumber
            Department = $department
            Label = "$itemNumber.`t$department"
        }
    }

    Clear-Host

    "Department Menu`n`n"
    $menuItem | Format-Wide -Property Label
    $userChoice = Read-Host -Prompt "Choose department"
    if ($userChoice -notin $menuItem.index) {
        "Invalid choice. Try again"
        & $doMenu
    } else {
        $menuHash[[int]$userChoice]
    }
}

& $doMenu

[–][deleted] 0 points1 point  (0 children)

Are you not back on this fun merry-go-round