'PowerShell - Defining multiple IF statements outcomes
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dcaz |
