This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
From a Newsgroups-post I came to this script to ask the users something with Helptext (after my original answer on a seemingly simple question, and a lot of help from others on that tread,
first warning me for the use of $? (will not be set to $true if Command is succesfull), then a CheckExitCode Function, and tips that did lead me to this.
I came to the following function.
Prompt-YesNo
$n = ([System.Management.Automation.Host.ChoiceDescription]"&No")
$n.helpmessage = "No, don't go on"
$Y = ([System.Management.Automation.Host.ChoiceDescription]"&Yes")
$y.helpmessage = "Yes, go on"
$YN= ($Y,$N)
Function Prompt-YesNo ($Caption = "Continue", $Message = "Do You wan't to go on",$choices = $YN){
$host.ui.PromptForChoice($caption,$Message,[System.Management.Automation.Host.ChoiceDescription[]]$choices,0)
}
so Now you can do this :
$answer = Prompt-YesNo
if ($answer -eq 0) {"go on"} else {"Stop"}
the result will look like this (normal the check line will not be seen ;-)
MSH>$answer = Prompt-YesNo
Continue
Do You wan't to go on
[Y] Yes [N] No [?] Help (default is "Y"): ?
Y - Yes, go on
N - No, don't go on
[Y] Yes [N] No [?] Help (default is "Y"):
MSH>if ($answer -eq 0) {"go on"} else {"Stop"}
go on
MSH>$answer = Prompt-YesNo
Continue
Do You wan't to go on
[Y] Yes [N] No [?] Help (default is "Y"): n
MSH>if ($answer -eq 0) {"go on"} else {"Stop"}
Stop
MSH>
I think this is a very cool way to get input from the user the same way as a CMDlet.
thanks from here to all that helped on the channel.
microsoft.public.windows.server.scripting
I think to newsgroup is a good learning start for MSH,
I think the help there is great !!
and helping other will bring you to ideas.
enjoy, and maybe see you on the NG
gr /\/\o\/\/