PowerShell/Scripts/locate-city.ps1

39 lines
872 B
PowerShell
Raw Normal View History

2020-11-01 12:11:15 +01:00
#!/snap/bin/powershell
2020-12-14 07:48:21 +01:00
# Syntax: ./locate-city.ps1 [<city>]
2020-11-01 12:20:55 +01:00
# Description: prints the lat/long coordinates of the given city
2020-11-01 12:11:15 +01:00
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([string]$City)
2020-11-06 15:30:14 +01:00
if ($City -eq "" ) {
2020-11-22 10:36:46 +01:00
$City = read-host "Enter the city"
2020-11-06 15:30:14 +01:00
}
2020-11-01 12:11:15 +01:00
try {
2020-11-06 15:30:14 +01:00
write-progress "Reading worldcities.csv..."
2020-11-01 12:11:15 +01:00
$Table = import-csv worldcities.csv
$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
2020-11-22 10:36:46 +01:00
write-host "* $City ($Country, $Region, population $Population) is at $Lat°N, $Long°W"
2020-11-01 12:11:15 +01:00
}
}
if ($FoundOne) {
exit 0
}
write-error "City $City not found"
exit 1
2020-12-09 10:30:55 +01:00
} catch {
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}