Powershell menu script

Do you ever need to give some input to a script? Maybe its a long filename or some other long and complex string, or maybe you’re just lazy like me? I wrote a script for handling the input, it’s a bit like the curses based menus you see in some network equipment, and in Linux systems. The eaxmple below is for getting the firmware baseline for a HP OneView system, but the menu is usable almost everywhere you need to specify something.

Lets stop talking, and take a look at the actual script:

param(
[string]$selection
)

if ((get-module hpone*).count -lt 1){
Get-Module -name hpone* -ListAvailable | Import-Module -WarningAction SilentlyContinue
}

if ($Global:connectedSessions){
    echo "Already connected..."
    } else {
    Connect-HPOVMgmt -Appliance ADDRESS -Credential (Get-Credential)
}

$global:MenuOptions = get-hpovbaseline

if ($Selection -eq ""){

    Clear-Host
    Write-Host "================Select Firmware Version================"
    
    $MenuNumber = 1
    foreach ($Option in $MenuOptions.version){

    Write-Host "$MenuNumber : $Option"
    $MenuNumber++
    }
    Write-Host -ForegroundColor red "Quit: Press 'ctrl-c' to quit."

    [int]$Selection = Read-Host "Please select firmware by number"
    
}

$baseline = $MenuOptions[$Selection -1]

The above code has some checks in the beginning to see if we have the right modules loaded, and if we are connected to the OneView server. This is not needed for the menu, but maybe it can be useful for you anyway.
In this example I’m listing the firmware included in the baselines from an HP OneView system, and the $baseline variable will have the selection I made.
This is usable for all kinds of interactions with other systems, e.g. its also very usable for vmware vCenter.
The selection parameter is there if you run this task often and already know what to select, then you can add it as an attribute to the script.

Please enjoy using the script.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.