Powershell Prompt for Input Yes No for Continuing or Leaving a Script

RRS feed

  • Remove From My Forums

 locked

validate read-host to yes or no RRS feed

  • Question

  • I want to use read-host, but I only want the input to be yes or no.

    How do I achieve this?

Answers

  • $answer = Read-Host "Yes or No"  while("yes","no" -notcontains $answer) { 	$answer = Read-Host "Yes or No" }

    • Marked as answer by Darren Welldon Wednesday, February 20, 2013 2:25 PM

All replies

  • $answer = Read-Host "Yes or No"  while("yes","no" -notcontains $answer) { 	$answer = Read-Host "Yes or No" }

    • Marked as answer by Darren Welldon Wednesday, February 20, 2013 2:25 PM
  • Thanks works a treat.

    I am new to powershell so still learning.

  • Why not use a popup box with Yes or No options?

    $shell = new-object -comobject "WScript.Shell" $result = $shell.popup("Do you want to?",0,"Question",4+32)

    $result will be 6 for yes, 7 for no.


    C Shane Cribbs
    http://www.georgiatechnologies.com


    • Edited by Shane Cribbs Wednesday, February 20, 2013 2:29 PM
  • in Powershell 3.0:

    [ValidateSet('Yes','No')]$Answer = Read-Host "Are You Ok?"

  • to these excellent suggestions, I'll add my own from here: http://gallery.technet.microsoft.com/PowerShell-Select-MenuItem-f3d4f012

    PS C:\> $Ans = select-menuitem -heading "demo" -prompt "enter only yes or no" -menutext 'yn' -default "no" demo enter only yes or no [Y] Yes  [N] No  [?] Help (default is "N"): PS C:\> $ans No PS C:\> $Ans = select-menuitem -heading "demo" -prompt "enter only yes or no" -menutext 'yn' -default "no" demo enter only yes or no [Y] Yes  [N] No  [?] Help (default is "N"): what [Y] Yes  [N] No  [?] Help (default is "N"): n PS C:\> $ans No PS C:\> $Ans = select-menuitem -heading "demo" -prompt "enter only yes or no" -menutext 'yn' -default "no" demo enter only yes or no [Y] Yes  [N] No  [?] Help (default is "N"): y PS C:\> $ans Yes PS C:\>

    The only user input allowed in the above example is "y", "yes", "n", or "no", however the function returns only "yes" or "no". Had the -default parameter been set to "n", the function would return "y" or "n"; had it been set to 1, it would return 0 or 1.

    Each suggestion has its own advantages and limitations. Blindrood's requires V3, and Shane's returns 6 for yes and 7 for no, and can only handle a fixed combination of possible answer sets. The -Menutext of my select-menuitem function allows a more custom approach:

    PS C:\> $Ans = select-menuitem -menutext "&Delete = delete file `n &Rename = rename file `n e&Xit = stop" -default 'rename' Response required: Enter your selection from the menu shown below: [D] Delete  [R] Rename  [X] eXit  [?] Help (default is "R"): ? D - delete file R - rename file X - stop [D] Delete  [R] Rename  [X] eXit  [?] Help (default is "R"): x PS C:\> $ans eXit PS C:\>

    It, too, has limitations, but I'll leave it to others to point them out. In any case, I'm not suggesting my suggestion is the best solution, as each has its place, depending on circumstances. I'm just offering it as an alternative... ;-)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

  • do { $answer = Read-Host "yes or no" }  until ("yes","no" -ccontains $answer)                  

  • EX. 1 A DO ... WHILE loop

    do{

    ...something

    }while($(Read-Host "Continue? [Y]es or [N]o").ToLower() -eq 'y')

    EX. 2  IF statement

    if($(Read-Host "Continue? [Y]es or [N]o").ToLower() -eq 'y'){

    ....

    }


    • Edited by Ely Pelowski Wednesday, December 5, 2018 9:48 PM
    • Proposed as answer by Ely Pelowski Wednesday, December 5, 2018 9:48 PM
  • Hi Shane,

    I love this approach, but could you please detail WHY this works the way it does? I am unable to ascertain why the output is 6 or 7, or what the significance of 4+32 is. I would love some clarity on this please!

    This was easy to find

    https://ss64.com/vb/popup.html



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

kanescess1975.blogspot.com

Source: https://social.technet.microsoft.com/Forums/windows/en-US/66c1fd29-ce8e-44bc-98a1-b8ff60b32e3e/validate-readhost-to-yes-or-no?forum=winserverpowershell

0 Response to "Powershell Prompt for Input Yes No for Continuing or Leaving a Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel