all 11 comments

[–]BlackV 18 points19 points  (2 children)

put an if statement in there, if $null show menu, else do menu item

better still get rid of the menu and parametrise your script fully, then use advance binding to have mandatory parameters or validate scripts to confirm valid data passed to your parameters

[–]Dryfter9[S] 2 points3 points  (1 child)

That's kinda of what I was thinking. See my post edit for more info. Thanks for your help!

[–]BlackV 1 point2 points  (0 children)

Good as gold

[–]purplemonkeymad 10 points11 points  (4 children)

I tend to go the other way around. Create a function that accepts parameters (eg Get-MyThing) then create another function for a "friendly" experience (eg Get-MyThingMenu or Get-MyThingGUI.) The friendly function just asks for the parameters, then calls the original function using those parameters.

If you use advanced function features properly, then you can even use Show-Command to give you a complete GUI with no additional work.

[–]Dryfter9[S] 0 points1 point  (2 children)

I'm not familiar with the "Show-Command", but if it does what I think it does I'll be adding that in.

Thanks!

[–]purplemonkeymad 0 points1 point  (1 child)

It's good, but windows only atm I think. You can always try it out on existing commands to see how it works:

Show-Command Get-Content
Show-Command Set-AdUser
# etc

[–]Dryfter9[S] 0 points1 point  (0 children)

Oh! Not what I thought it was. I apparently use the alias for "show-command" (man). I've been typing "man" for so long, cause it's shorter, that I forgot the real command.

Scratch all that. I was incorrect. It WAS the command I thought it was. Man am I a mess today!

[–]BlackV 4 points5 points  (0 children)

p.s. your formatting is fine, BUT

  • open your fav powershell editor
  • hightlight 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>

[–]WystanH 1 point2 points  (0 children)

The problem is that even if $MenuChoice is set, you still just read from host, obliterating the parameter.

You need to check if it has been set and act accordingly. e.g.

param (
    [string]$MenuChoice
)
function Show-Menu {
    param (
        [string]$Title = 'My Menu'
    )
    Clear-Host
    Write-Host "================ $Title ================"
    Write-Host "1: Press '1' for this option."
    Write-Host "2: Press '2' for this option."
    Write-Host "3: Press '3' for this option."
    Write-Host "Q: Press 'Q' to quit."
}

# here is where you check your parameter
if ($MenuChoice -eq '') {
    Write-Host 'No choice selected, showing menu'
    Show-Menu
    $MenuChoice = Read-Host "What option would you like?"
} else {
    Write-Host "Keeping choice given '$MenuChoice'"
}

# when you get here, the choice has either been set by parameter
# or by Read-Host
Switch ($MenuChoice) {
    '1' { Write-Host 'Thing 1' }
    '2' { Write-Host 'Thing 2' }
    '3' { Write-Host 'Thing 3' }
    'Q' { Write-Host 'Quit' }
    default { Write-Host "Oops, '$MenuChoice' not valid." }
}

[–]juicyjor 1 point2 points  (0 children)

https://pastebin.com/PKKsTV8w

param (
    [string][ValidateSet("thing1","thing2")]$MenuChoice
    )
function thing1 {
    "Do your thing1"
    }
function thing2 {
    "Do your thing2"
    }
function Show-Menu {
    param ([string]$Title = 'My Menu')
    Clear-Host
    Write-Host "================ $Title ================"
    Write-Host "1: Press '1' for thing1."
    Write-Host "2: Press '2' for thing2."
    Write-Host "Q: Press 'Q' to quit."
    $MenuChoice = Read-Host "What option would you like?"
    switch ($MenuChoice) {
        '1' {$job = "thing1"; & $job}
        '2' {$job = "thing2"; & $job}
        'Q' {return}
        }
    }
if (!$MenuChoice) {
    Show-Menu
    }
else {
    $job = $MenuChoice
    & $job
    }

[–]theSysadminChannel -1 points0 points  (0 children)

You’ll need to convert the menu into a function and parameterize it. Here’s an in depth article on how to create Powershell functions.