Update locate-city.ps1

This commit is contained in:
Markus Fleschutz 2024-03-28 10:06:26 +01:00
parent aed2b7d940
commit 6e05236a99

View File

@ -3,43 +3,41 @@
Prints the geographic location of a city Prints the geographic location of a city
.DESCRIPTION .DESCRIPTION
This PowerShell script prints the geographic location of the given city. This PowerShell script prints the geographic location of the given city.
.PARAMETER City .PARAMETER city
Specifies the city to look for Specifies the name of the city to look for
.EXAMPLE .EXAMPLE
PS> ./locate-city.ps1 Paris PS> ./locate-city.ps1 Amsterdam
* Amsterdam (United States, New York, population 21241) is at 42.9420°N, -74.1907°W
* Amsterdam (Netherlands, Noord-Holland, population 1031000) is at 52.3500°N, 4.9166°W
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$City = "") param([string]$city = "")
try { try {
if ($City -eq "" ) { $City = Read-Host "Enter the city name" } if ($city -eq "" ) { $city = Read-Host "Enter the name of the city" }
Write-Progress "Reading worldcities.csv..." Write-Progress "Reading data/worldcities.csv..."
$Table = import-csv "$PSScriptRoot/../data/worldcities.csv" $table = Import-CSV "$PSScriptRoot/../data/worldcities.csv"
$FoundOne = 0 $foundOne = 0
foreach($Row in $Table) { foreach($row in $table) {
if ($Row.city -eq $City) { if ($row.city -eq $city) {
$FoundOne = 1 $foundOne = 1
$Country = $Row.country $country = $row.country
$Region = $Row.admin_name $region = $row.admin_name
$Lat = $Row.lat $lat = $row.lat
$Long = $Row.lng $long = $row.lng
$Population = $Row.population $population = $row.population
write-host "* $City ($Country, $Region, population $Population) is at $Lat°N, $Long°W" Write-Host "* $city ($country, $region, population $population) is at $lat°N, $long°W"
} }
} }
if (-not $foundOne) { throw "No city '$city' found in database" }
if ($FoundOne) {
exit 0 # success exit 0 # success
}
write-error "City $City not found"
exit 1
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error $($_.InvocationInfo.ScriptLineNumber): $($Error[0])."
exit 1 exit 1
} }