0

All I'm trying to do is to see if the user input $month is in the array $months. But it's not liking something. Help?

Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = @("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
    $month = read-host "Enter the month";
    if ($months -Contains $month)
    {
        write-host "Invalid entry"
        $finished = $false
    }
    else
    {
        $finished = $true
    }
}

2 Answers 2

3

You test logic is just not the good one, just reverse youy test or reverse your actions:

Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = @("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
    $month = read-host "Enter the month";
    if ($months -Contains $month)
    {
        $finished = $true
    }
    else
    {
        write-host "Invalid entry"
        $finished = $false
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using -Contains you should just run a RegEx match using the -Match operator. Or, as you are currently testing for a negative result, use -notmatch instead. You can use your existing code, just modify it a little by joining your months with a pipe character. Like:

Write-host "The script is to collect from the user High Tempature and Low Tempature for a day in degrees F."
$months = @("January", "February","March","April","May","June","July","August","September","October","November","December")
$finished = $false
while ($finished -eq $false){
    $month = read-host "Enter the month";
    if ($month -notmatch ($months -join "|"))
    {
        write-host "Invalid entry"


     $finished = $false
    }
    else
    {
        $finished = $true
    }
}

Better yet, let's get rid of the If/Else and shorten this. Move the Join to where we define $Months, and then ask for a month and if it isn't a match ask for it again until it is with a While.

$months = @("January", "February","March","April","May","June","July","August","September","October","November","December") -join '|'
$month = read-host "Enter the month"
While($month -notmatch $months){
    "Invalid Entry"
    $month = read-host "Enter the month"
}

4 Comments

@JPBlanc It works if he does it right, yes, but isn't he going to run into case sensitivity issues? Or is -contains case insensitive?
CContains is case sensitive, not Contains.
I don't think switching a contains to a regex is a good trade.
@MikeShepard it's been shown that comparing a RegEx match such as this has better performance than using the -contains operator, so this is a fairly common practice I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.