PS CLI Menu

Tagged: , ,

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Anthony Anthony 3 months, 3 weeks ago.

  • Author
    Posts
  • #49966
    Profile photo of Rafal Niznik
    Rafal Niznik
    Participant

    Hi Guys
    I have a simple script presented below now I want to add CLI menu for example:
    By pressing 1 to 5 script will preform different assigned function
    After selecting option are reciving results by pressing any button it will return to main menu
    By pressing Q script will close menu and return to PS
    I also want this to be saved as module so it can be imported on different maachine
    What would be best solution to create such a menu

    Regards
    Raf

    function Get-RemoteDeviceInfo
    {
    [CmdletBinding()]

    param(
    [Parameter(Mandatory=$True)]
    [string[]]$ComputerName
    )

    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $ComputerName
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $ComputerName
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $ComputerName
    $computerCPU = get-wmiobject Win32_Processor -Computer $ComputerName
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='C:'"

    ## write-host option displays detailed data

    write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkGreen
    "——————————————————-"
    " Manufacturer: " + $computerSystem.Manufacturer
    " Model: " + $computerSystem.Model
    " Serial Number: " + $computerBIOS.SerialNumber
    " CPU: " + $computerCPU.Name
    " HDD Capacity C: " + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB" + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
    " RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
    " Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
    " User logged In: " + $computerSystem.UserName
    " Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
    }

  • #50004
    Profile photo of Edmond Yee
    Edmond Yee
    Participant

    You should create a While loop with a condition of "not 'q'" and then inside the While, type out your list of options 1-5 to pick from, add a read-host line to get the user's choice, then create a Switch statement to tell the script where to go.

    $answer = $null
    While ($answer -ne 'q')
    {
       Write-Host '1. do this'
       Write-Host '2. do that'
       Write-Host '3. do this and that'
       Write-Host '4. Other'
       Write-Host '5. Quit'
       $answer = Read-Host 'Please select an option'
       Switch ($answer)
       {
           '1' {'command or function call for option 1'}
           '2' {'command or function call for option 2'}
           '3' {'command or function call for option 3'}
           '4' {'command or function call for option 4'}
           '5' {exit}#or possibly return depending on what else you are doing
           Default {Write-Host 'Input not understood, exiting'; Exit;}
       }
    }#end While
    
    • This reply was modified 3 months, 4 weeks ago by Profile photo of Edmond Yee Edmond Yee. Reason: forgot option 5
    • This reply was modified 3 months, 4 weeks ago by Profile photo of Edmond Yee Edmond Yee.
  • #50100
    Profile photo of Rafal Niznik
    Rafal Niznik
    Participant

    Edmond

    Thank you very much for your help on this one this is what I wanted.
    But I will need to add two more things.
    Currently when I select the option script will run and return to main menu.
    What I would like to change is:
    When I select the option it will run script and after that I will get the message:
    "Press Any button to continue..."
    After pressing any button this can return to main menu
    Anothre thing I would like to add is sub-menu so I can select first cathegory and under that category I will have different options to run. When I enter wrong section possibility to return to main menu.

    Thanks
    Raf

    • This reply was modified 3 months, 4 weeks ago by Profile photo of Rafal Niznik Rafal Niznik.
    • #50345
      Profile photo of Edmond Yee
      Edmond Yee
      Participant

      To make it ask to press any key to continue, you can just add a 'Pause' at the end of the while statement.

      The sub menu structure would be the same as the top level, just nested inside of the switch statement. You could also accomplish this with If statements and nesting those, however I prefer switch statements when dealing with more than a simple Y/N answer.

  • #50103
    Profile photo of Rafal Niznik
    Rafal Niznik
    Participant

    ..

    • This reply was modified 3 months, 4 weeks ago by Profile photo of Rafal Niznik Rafal Niznik.
  • #50559
    Profile photo of Anthony
    Anthony
    Participant

    Here is an example of one of my scripts where I did something similar to what you are trying to achieve a while back. You might be able to get some ideas from it.

    https://gist.github.com/theonlyway/710d93d9731e530bcd512ea01d446a2f

    • This reply was modified 3 months, 3 weeks ago by Profile photo of Anthony Anthony. Reason: Don't need the forum to show the entire gist
    • This reply was modified 3 months, 3 weeks ago by Profile photo of Anthony Anthony.

You must be logged in to reply to this topic.