How do I validate user input in a loop?

Mckibblesydoo

I'm a student and I need to write a script in Powershell ISE that will provide the user with a menu and then display OS information based on their selection or quit if they enter 'q'

I've got the menu input/output loop working but I'm also required to make sure they selected a valid option and reprompt them if not.

It outputs the invalid message no matter what the user input value is.

So if the user enters 1 it will return:

Invalid input, please select an option from the menu @{OsName=Microsoft Windows Server 2016 Standard} Press Enter to continue...:

I have no background in programming so my understanding of powershell and coding is very basic and most search results have provided examples that are beyond the scope of my knowledge!

function sysInfoMenu {

    param (
    [string] $Title = 'System Information'
    )

    Clear-Host

    Write-Host "=============== $Title ==============="
    "`n`n"
    Write-Host "Please select from the following options:"
    "`n"
    Write-Host "Press 1 for: OS System"
    Write-Host "Press 2 for: OS Type"
    Write-Host "Press 3 for: OS Architecture (32 or 64 bit)"
    Write-Host "Press 4 for: OS Version"
    Write-Host "Press 5 for: OS Bios Version"
    Write-Host "Press 6 for: Computer Brand"
    Write-Host "Press 7 for: Computer Name"
    Write-Host "Press 8 for: Current Domain"
    Write-Host "Press 9 for: Computer Model"
    Write-Host "Press 10 for: Current User"
    Write-Host "Press 11 for: Timezone"
    Write-Host "Press 'q' to quit."
}

do {
    sysInfoMenu
    $info = $null
    $UserInput = Read-Host "Please select an option from the menu"

        
    switch ($UserInput)
    {
      '1' {$info = Get-ComputerInfo -Property OSName}
      '2' {$info = Get-ComputerInfo -Property OSType}
      '3' {$info = Get-ComputerInfo -Property OSArchitecture}
      '4' {$info = Get-ComputerInfo -Property OSVersion}
      '5' {$info = Get-ComputerInfo -Property BiosVersion}
      '6' {$info = Get-ComputerInfo -Property CsManufacturer}
      '7' {$info = Get-ComputerInfo -Property CsName}
      '8' {$info = Get-ComputerInfo -Property CsDomain}
      '9' {$info = Get-ComputerInfo -Property CsModel}
      '10' {$info = Get-ComputerInfo -Property CsUserName}
      '11' {$info = Get-ComputerInfo -Property TimeZone}
    } 
          
          
       if ($UserInput -lt 1 -gt 11 -ne 'q'){
       Write-Host "Invalid input, please select an option from the menu"
       } 
      
  Write-Host $info
  pause
       
        
}
until ($UserInput -eq 'q') 

RetiredGeek

You have a couple of structural problems.

  • You didn't terminate your function.
  • You didn't call your function.
  • You keep calling Get-ComputerInfo which is slow and only needs to be done once.
  • Your menu needs to be in your loop so you can clear the screen and get rid of the previous answers and/or error messages.

Here's some alternative code which I've commented to help you learn.

function SysInfoMenu {

    param (
     [Parameter(Mandatory=$False)]
      [string] $Title = 'System Information'
    )

#Setup:

   $ByeBye   = $False           #Set initial value.
   #Hide the Progress Bar. Remove line if not wanted.
   $ProgressPreference = "SilentlyContinue"
   $CompInfo = Get-ComputerInfo #You only need to call once.

#Main Function Code:

do {
    Clear-Host

    Write-Host "=============== $Title ==============="
    "`n`n"
    Write-Host "Please select from the following options:"
    "`n"
    Write-Host "Press  1 for: OS System"
    Write-Host "Press  2 for: OS Type"
    Write-Host "Press  3 for: OS Architecture (32 or 64 bit)"
    Write-Host "Press  4 for: OS Version"
    Write-Host "Press  5 for: OS Bios Version"
    Write-Host "Press  6 for: Computer Brand"
    Write-Host "Press  7 for: Computer Name"
    Write-Host "Press  8 for: Current Domain"
    Write-Host "Press  9 for: Computer Model"
    Write-Host "Press 10 for: Current User"
    Write-Host "Press 11 for: Timezone"
    Write-Host "Press 'q' to quit."

    $UserInput = Read-Host "Please select an option from the menu"
        
 #Note: use of Break to leave switch once match is found.
       
    switch ($UserInput) {
       '1' {$CompInfo.OSName         ;Break}
       '2' {$CompInfo.OSType         ;Break}
       '3' {$CompInfo.OSArchitecture ;Break}
       '4' {$CompInfo.OSVersion      ;Break}
       '5' {$CompInfo.BiosVersion    ;Break}
       '6' {$CompInfo.CsManufacturer ;Break}
       '7' {$CompInfo.CsName         ;Break}
       '8' {$CompInfo.CsDomain       ;Break}
       '9' {$CompInfo.CsModel        ;Break}
      '10' {$CompInfo.CsUserName     ;Break}
      '11' {$CompInfo.TimeZone       ;Break}
      'q'  {$ByeBye = $True          ;Break}
      default {"Invalid input, please select an option from the menu"}
    } #End Switch      
          
    if (-not $ByeBye) { pause }
        
  } until ($ByeBye) #Exit via $ByeBye value!

} #End Function SysInfoMenu

SysInfoMenu #Call your function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i validate integer as user input?

how do I design a loop that accepts user input for a golfer name and their score then displays list?

How do I validate the password of a user created with the User Manager?

How do I use recursion to loop my code (when user enters invalid input, it prompts them again)?

I am trying to validate the user input. But if i entered an invalid character the program goes to an infinite loop

How do I correctly use a while-loop to fill 2 Arraylists with user input?

How do i validate user input that starts with 2 numbers?

How do I get user input to use my validate method for a login console application?

How do I validate a user's input and make sure it is the correct type and within a given range?

How do I remove a Map key that depends on user input inside a for loop?

How do I make a loop for user input in python?

How do I return the user input value inside of a while loop?

How to validate user input in python

Jquery how do I validate input field

How can I validate user-input in python?

How do I get a user input inside a `for in` loop

How to validate user input in shiny

validate user input and loop until correct

How do i use user input for a string array that is in a while loop?

ReactJS - How do I validate input fields

How do i validate a user input in python?

How to do while loop for each user input?

How do I break the while loop with user input

How do I loop a function after receiving user input?

How do I validate a user input with int.TryParse to ensure the user entered an integer but also ensure the number was between 1 and 4

How can I validate a Perl regex in user input?

How to validate user input in Java?

How do I use a function to validate user input within another function?

How do I exit a for or a while loop with user input in java