PowerShell/scripts/locate-city.ps1

44 lines
1.2 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-16 16:50:10 +02:00
Prints the geographic location of a city
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script prints the geographic location of the given city.
2024-03-28 10:06:26 +01:00
.PARAMETER city
Specifies the name of the city to look for
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-03-28 10:06:26 +01:00
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
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-11-01 12:11:15 +01:00
2024-03-28 10:06:26 +01:00
param([string]$city = "")
2021-02-18 20:17:55 +01:00
2020-11-01 12:11:15 +01:00
try {
2024-03-28 10:06:26 +01:00
if ($city -eq "" ) { $city = Read-Host "Enter the name of the city" }
2021-07-15 15:51:22 +02:00
2024-03-28 10:06:26 +01:00
Write-Progress "Reading data/worldcities.csv..."
$table = Import-CSV "$PSScriptRoot/../data/worldcities.csv"
2020-12-25 11:30:43 +01:00
2024-03-28 10:06:26 +01:00
$foundOne = 0
foreach($row in $table) {
if ($row.city -eq $city) {
$foundOne = 1
$country = $row.country
$region = $row.admin_name
$lat = $row.lat
$long = $row.lng
$population = $row.population
Write-Host "* $city ($country, $region, population $population) is at $lat°N, $long°W"
2020-11-01 12:11:15 +01:00
}
}
2024-03-28 10:06:26 +01:00
if (-not $foundOne) { throw "No city '$city' found in database" }
exit 0 # success
2020-12-09 10:30:55 +01:00
} catch {
2024-03-28 10:06:26 +01:00
"⚠️ Error $($_.InvocationInfo.ScriptLineNumber): $($Error[0])."
2020-12-09 10:30:55 +01:00
exit 1
}