2021-02-09 19:30:45 +01:00
|
|
|
#!/bin/powershell
|
2020-12-29 15:14:21 +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-16 12:03:47 +01:00
|
|
|
|
2021-01-02 11:08:59 +01:00
|
|
|
param([string]$CountryCode = "", [string]$ZipCode = "")
|
|
|
|
$PathToRepo = "$PSScriptRoot/.."
|
2020-12-25 11:30:43 +01:00
|
|
|
|
2020-12-16 12:03:47 +01:00
|
|
|
try {
|
2020-12-25 11:30:43 +01:00
|
|
|
if ($CountryCode -eq "" ) {
|
|
|
|
$CountryCode = read-host "Enter the country code"
|
|
|
|
}
|
|
|
|
if ($ZipCode -eq "" ) {
|
|
|
|
$ZipCode = read-host "Enter the zip code"
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:03:47 +01:00
|
|
|
write-progress "Reading zip-codes.csv..."
|
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 {
|
2020-12-25 11:30:43 +01:00
|
|
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2020-12-16 12:03:47 +01:00
|
|
|
exit 1
|
|
|
|
}
|