PowerShell command line menu

I’ve been working on a server build script to assist our server team in automating their server build outs. The objective was to select which environment the server is being built in (Alpha, Dev, etc.) then the type of server to build (Database, Web, etc.) and from those two choices go about setting proper permissions and server functionality. Normally when I build tools for other teams, I’ll use Sapien’s PowerShell Studio to build WinForm apps, but this time the server team wanted to be able to crack open the file and modify it when needed, so I couldn’t hand over a compiled exe file this time. So, since I couldn’t do a GUI, I had to come up with a way of doing a “drop down” menu at the command line and this is what I came up with.

function menu1 {
Clear-Host
Write-Host "=============================================================
1) Alpha
2) AlphaX
3) Dev
4) DevInt
============================================================="
}

function menu2 {
Clear-Host
Write-Host "=============================================================
1) Web
2) Database
3) SSIS
4) ServiceMart
5) Microstrategy
============================================================="
}


$environment = ""
$serverType = ""

$stoploop = $false
$retryCount = 0
menu1
#do loop to validate menu input
do {
    $input1 = Read-Host "Please select a environment type"
    if (($input1 -ge 1) -and ($input1 -le 4)) {$stoploop = $true}
    else {
        Write-Host "ERROR! Please select 1-4"
        $retryCount = $retryCount + 1
        if ($retryCount -ge 3) {
            Write-Host "Seriously, how hard is it to press 1-4?"
            $stoploop = $true
        }
    }
} while ($stoploop -eq $false)

switch ($input1) 
{
    1 { $script:environment = "Alpha" }
    2 { $script:environment = "AlphaX" }
    3 { $script:environment = "Dev" }
    4 { $script:environment = "DevInt" }
}

$stoploop = $false
$retryCount = 0
menu2
#do loop to validate menu input
do {
    $input2 = Read-Host "Please select a server type"
    if (($input2 -ge 1) -and ($input2 -le 5)) {$stoploop = $true}
    else {
        Write-Host "ERROR! Please select 1-5"
        $retryCount = $retryCount + 1
        if ($retryCount -ge 3) {
            Write-Host "Seriously, how hard is it to press 1-5?"
            $stoploop = $true
        }
    }
} while ($stoploop -eq $false)

switch ($input2) 
{
    1 { $script:serverType = "Web" }
    2 { $script:serverType = "Database" }
    3 { $script:serverType = "SSIS" }
    4 { $script:serverType = "ServiceMart" }
    5 { $script:serverType = "Microstrategy" }
}

#if either menu items fail, exit
if ($environment -eq "" -or $serverType -eq "") {
    Write-Host "You suck at this."
    Read-Host
    exit
    }
else {Write-Host "You selected $environment and $serverType"}
Advertisements
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s