Radio Button Creation

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5063

    by dagr8tim at 2012-08-31 09:16:42

    I just started rewriting some of my bat scripts that I use for minor or time consuming tasks to power shell. While doing that, I realized I have some duplicate scripts. I manage multiple tiers of servers and have to routinely perform what is the same task on different tiers. A simple example is restarting a service on a remote machine. Up until now, I have had a script for each tier. I'd like to find a way that when you launch the script, you get prompted with a radio button, and based on your selection it performs the task in that environment.

    I've been reading everything I can find and I can't find anyone that shows the basic syntax for doing this. Does anyone have a code snippet I can borrow that illustrates this?

    by DonJ at 2012-08-31 09:30:36

    Well... it's a bit more complex than that. You need to create the dialog (form), populate it with the radio button and any labels, put an Ok button, code that button... it's fairly involved. Have you looked at a tool like PowerShell Studio from SAPIEN? It's designed to do that kind of GUI-flavored stuff, and make it a bit easier.

    Alternately... if you're looking for just a text-based multiple-choice prompt in PowerShell, that's straightforward.

    by dagr8tim at 2012-08-31 09:32:56

    [quote="DonJ"]Well... it's a bit more complex than that. You need to create the dialog (form), populate it with the radio button and any labels, put an Ok button, code that button... it's fairly involved. Have you looked at a tool like PowerShell Studio from SAPIEN? It's designed to do that kind of GUI-flavored stuff, and make it a bit easier.

    Alternately... if you're looking for just a text-based multiple-choice prompt in PowerShell, that's straightforward.[/quote]

    I realize I could manually type the name or some varient of it and get what I need. I may try that afterall. I was hoping this was like 5 lines of code.

    by poshoholic at 2012-08-31 09:40:23

    The answer to this depends on how many choices you have. If you have a small number of choices, you can do this:

    [script=powershell]$choice = $host.UI.PromptForChoice('What do you want to do?','Indicate what you want to do by choosing one of the following options]]@('Watch a &movie','Play a &game','Do some &work','Get some &sleep'),2)
    switch ($choice) {
    0 {
    'I love movies. What are we going to watch?'
    break
    }
    1 {
    Start-Process 'http://www.moviesounds.com/wargames/chess2.wav'
    break
    }
    2 {
    'Really? Booooring.'
    break
    }
    3 {
    foreach ($second in 1..10) {
    Write-Progress -Activity 'Sleeping...' -Status ('Z'*$second)
    Start-Sleep -Seconds 1
    }
    'Was that long enough?'
    break
    }
    }[/script]
    If you have many choices though or if you want the choices listed vertically, you'll have to use another mechanism that accepts any input, like this:

    [script=powershell]$choice = Read-Host -Prompt @'
    Indicate what you want to do by choosing one of the following options:
    [M] Watch a movie
    [G] Play a game
    [W] Do some work (Default)
    [S] Get some sleep
    What do you want to do?
    '@
    switch ($choice) {
    'M' {
    'I love movies. What are we going to watch?'
    break
    }
    'G' {
    Start-Process 'http://www.moviesounds.com/wargames/chess2.wav'
    break
    }
    'W' {
    'Really? Booooring.'
    break
    }
    'S' {
    foreach ($second in 1..10) {
    Write-Progress -Activity 'Sleeping...' -Status ('Z'*$second)
    Start-Sleep -Seconds 1
    }
    'Was that long enough?'
    break
    }
    default {
    'I''m sorry, Dave. I''m afraid I can''t do that.'
    break
    }
    }[/script]
    With this approach, it's up to you to handle looping if you want for invalid input. You will probably also want an option to exit the choice selection process if it's going to loop until it gets something valid.

    by dagr8tim at 2012-08-31 10:02:08

    I have 2 to 3 choices per script. Thanks for the code snippet.

You must be logged in to reply to this topic.