locked
Newbie Script - Read Host Invalid Option RRS feed

  • Question

  • Hey folks,

    Absolutely newbie here and trying to brush up my basic powershell skills.

    ###Define Variables###
    $date = Get-Date
    $Time = "Time: " + $time.ToShortTimeString()
    $Computername = $env:computername
            
    
    
    function show-menu
    {
        param (
            [string]$Title = 'My Menu'
        )
        cls
        Write-Host "===============$Title==============="
        Write-Host "1: Press 1 to get date"
        Write-Host "2: Press 2 to get time"
        Write-Host "3: Press 3 to get host"
        Write-Host "4: Press Q to quit"
    }
    
    do
    {
        Show-Menu
        $input = Read-Host "Please make a selection"
        switch ($input)
        {
            '1'{
               cls
               'You chose option #1'
               Write-host $date
             }'2'{
                cls
                'You chose option #2'
                write-host $time
             }'3'{
                cls
                'You chose option #3'    
                write-host "This Computer's Hostname is $computername"
             }'Q'{
                return
             }
        }
        pause
    }
    until ($input -eq 'q')

    So what I have here is keys 1-3 are defined to display different information. What I want to do is define keys 4-9 as 'Invalid Response'. I know I can keep defining each digit separately but I am wondering if there's a way to define it somehow the following...

    '4-9'{
                cls
                'You chose an invalid option'
                write-host Invalid Response'

    Hope I am explaining it in a proper way, but totally newbie here so apologize in advance :)

    Thanks

    Thursday, August 24, 2017 2:14 PM

Answers

  • Ok, I tried but my script doesn't make sense anymore :)
    • Marked as answer by techlambo Thursday, August 24, 2017 4:32 PM
    Thursday, August 24, 2017 3:50 PM

All replies

  • You could just use a IF statement instead of your switch statement. IF and IFELSE for 1-3 and Q and ELSE for "invalid option".

    Thursday, August 24, 2017 2:28 PM
  • Switch statement has wildcard & regex parameters

    Read this

    https://technet.microsoft.com/en-us/library/ff730937.aspx

    https://ss64.com/ps/switch.html

    Thursday, August 24, 2017 3:49 PM
  • Ok, I tried but my script doesn't make sense anymore :)
    • Marked as answer by techlambo Thursday, August 24, 2017 4:32 PM
    Thursday, August 24, 2017 3:50 PM
  • Hi,

    you could do it by using the default statement:

    switch ($input)
        {
            '1'{
               cls
               'You chose option #1'
               Write-host $date
             }'2'{
                cls
                'You chose option #2'
                write-host $time
             }'3'{
                cls
                'You chose option #3'    
                write-host "This Computer's Hostname is $computername"
             }'Q'{
                return
             } default {
                 Write-Warning "Invalid input: $input"
             }
        }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, August 24, 2017 3:52 PM