PowerShell/Scripts/locate-zip-code.ps1

46 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-02-09 19:30:45 +01:00
#!/bin/powershell
2020-12-29 15:14:21 +01:00
<#
2021-03-22 20:10:18 +01:00
.SYNTAX ./locate-zip-code.ps1 [<country-code>] [<zip-code>]
.DESCRIPTION prints the geographic location of the given zip-code
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-12-16 12:03:47 +01:00
2021-02-18 20:17:55 +01:00
param($CountryCode = "", $ZipCode = "")
2020-12-25 11:30:43 +01:00
2021-02-18 20:17:55 +01:00
if ($CountryCode -eq "" ) {
$CountryCode = read-host "Enter the country code"
}
if ($ZipCode -eq "" ) {
$ZipCode = read-host "Enter the zip code"
}
2020-12-25 11:30:43 +01:00
2021-02-18 20:17:55 +01:00
try {
2020-12-16 12:03:47 +01:00
write-progress "Reading zip-codes.csv..."
2021-02-18 20:17:55 +01:00
$PathToRepo = "$PSScriptRoot/.."
2020-12-25 11:30:43 +01:00
$Table = import-csv "$PathToRepo/Data/zip-codes.csv"
2020-12-16 12:03:47 +01:00
$FoundOne = 0
foreach($Row in $Table) {
if ($Row.country -eq $CountryCode) {
if ($Row.postal_code -eq $ZipCode) {
2020-12-16 12:11:01 +01:00
$Country=$Row.country
2020-12-16 12:03:47 +01:00
$City = $Row.city
$Lat = $Row.latitude
$Lon = $Row.longitude
2020-12-25 11:30:43 +01:00
write-output "* $Country $ZipCode $City is at $Lat°N, $Lon°W"
2020-12-16 12:03:47 +01:00
$FoundOne = 1
}
}
}
if ($FoundOne) {
exit 0
}
write-error "Zip-code $ZipCode in country $CountryCode not found"
exit 1
} catch {
2021-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-16 12:03:47 +01:00
exit 1
}