The Multifunctioning DBA

Sep 28 2012   5:05PM GMT

Poweshell and user input driven menu

Colin Smith Colin Smith Profile: Colin Smith

I needed to be able to display options to users and let them make simple choices..  to make a selection of what to do. I wrote the following in order to accommodate my needs. So I display a list of choices and the user makes a selection. After the selection is made the script calls the function that is associated with the choice.

 

Here you go

 

$i = 0
$functions = “Function_1″, “Function_2″, “Function_3″, “Function_4″, “Exit”
$funarray = @()
foreach ($function in $functions)
{
$i++
$funarray += $i
$funarray += $function
Echo “$i $function”
}
$Selection = Read-Host “`nMake Selection”
$av = 0
echo “`n”
foreach ($value in $funarray)
{
if ($selection -eq $funarray[$av])
{
$av++

if ($funarray[$av] -eq “Exit”){exit}

& $funarray[$av]

break
}

$av++

}
“`n”

 

So if you run this here is what you will get.

Powershell Menu

Display a simple menu to user

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: