Home > front end >  PowerShell - Defining multiple IF statements outcomes
PowerShell - Defining multiple IF statements outcomes

Time:02-26

I'm having trouble with the below script. My goal is to have $city be checked by all of the if statements below and return to me my correct $region. Currently it keeps returning back "NA-West" because that is the last if statement I have. Any help is appreciated, thank you

$city = "New York"
if ($city -eq "Bangalore" -or "Hong Kong" -or "Jakarta" -or "Melbourne" -or "New Delhi" -or "Seoul" -or "Shanghai" -or "Sydney" -or "Taipei City" -or "Tokyo") {
$region = "APAC"
}
if ($city -eq "Hamburg" -or "London" -or "Madrid" -or "Milan" -or "Paris" ) {
$region = "EMEA"
}
if ($city -eq 'Chicago' -or 'Detroit' -or 'New York' -or 'Toronto') {
$region = "NA-East"
}
if ($city -eq "Bellevue" -or "Boulder" -or "Denver" -or "Irvine" -or "Los Angeles" -or "San Francisco" -or "San Jose" -or "Santa Barbara" -or "Seattle" -or "Ventura") {
$region = "NA-West"
}
Write-Host "This is the region: " $region
Write-Host "This is the city: " $city

CodePudding user response:

This doesn't work as expected because all of your if conditions are always true - so $region = "NA-West" will always execute last.

The reason is that the -or operator is a boolean logic operator - it takes two operands and evaluates whether at least 1 of them is "truthy", by attempting to convert either value to a [bool] (eg. $true or $false).

When you have an expression like this:

$city -eq "Bellevue" -or "Boulder"

PowerShell effectively interprets it as:

[bool]($city -eq "Bellevue") -or [bool]("Boulder")

When PowerShell converts a string to a [bool], the rule is that empty strings are converted to $false, whereas non-empty strings (like "Boulder") are converted to $true.

So your last if statement has the exact same effect as if you had written:

if ($city -eq "Bellevue" -or $true -or $true -or $true -or $true -or $true -or $true -or $true -or $true -or $true) {

You need to either keep restating the comparison you want to make:

if($city -eq "Bellevue" -or $city -eq "Boulder" -or $city -eq "Denver" -or $city -eq "Irvine" -or $city -eq "Los Angeles" -or $city -eq "San Francisco" -or $city -eq "San Jose" -or $city -eq "Santa Barbara" -or $city -eq "Seattle" -or $city -eq "Ventura")

Or, better yet, use the -contains/-in operators instead:

if($city -in "Bellevue", "Boulder", "Denver", "Irvine", "Los Angeles", "San Francisco", "San Jose", "Santa Barbara", "Seattle", "Ventura")

CodePudding user response:

given your code it would be

$city = "New York"
if ($city -eq "Bangalore" -or $city -eq "Hong Kong" -or $city -eq "Jakarta" -or $city -eq "Melbourne" -or $city -eq "New Delhi" -or $city -eq "Seoul" -or $city -eq "Shanghai" -or $city -eq "Sydney" -or $city -eq "Taipei City" -or $city -eq "Tokyo") {
    $region = "APAC"
}
if ($city -eq "Hamburg" -or $city -eq "London" -or $city -eq "Madrid" -or $city -eq "Milan" -or $city -eq"Paris" ) {
    $region = "EMEA"
}
if ($city -eq 'Chicago' -or $city -eq 'Detroit' -or $city -eq 'New York' -or $city -eq 'Toronto') {
    $region = "NA-East"
}
if ($city -eq "Bellevue" -or $city -eq "Boulder" -or $city -eq "Denver" -or $city -eq "Irvine" -or $city -eq "Los Angeles" -or $city -eq "San Francisco" -or $city -eq "San Jose" -or $city -eq "Santa Barbara" -or $city -eq "Seattle" -or $city -eq "Ventura") {
    $region = "NA-West"
}
Write-Host "This is the region: " $region
Write-Host "This is the city: " $city
  • Related