Added locate-zip-code.ps1

This commit is contained in:
Markus Fleschutz
2020-12-16 11:03:47 +00:00
parent 0594220187
commit d80ef764d7
5 changed files with 1129950 additions and 4 deletions

View File

@ -1,7 +1,7 @@
#!/snap/bin/powershell
# Syntax: ./locate-city.ps1 [<city>]
# Description: prints the lat/long coordinates of the given city
# Description: prints the geographic location of the given city
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0

View File

@ -1,7 +1,7 @@
#!/snap/bin/powershell
# Syntax: ./locate-ipaddress.ps1 [<IPaddress>]
# Description: locates the geographic position of the given IP address
# Description: prints the geographic location of the given IP address
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0

44
Scripts/locate-zip-code.ps1 Executable file
View File

@ -0,0 +1,44 @@
#!/snap/bin/powershell
# Syntax: ./locate-zip-code.ps1 [<country-code>] [<zip-code>]
# Description: prints the geographic location of the given zip-code
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([string]$CountryCode, [string]$ZipCode)
if ($CountryCode -eq "" ) {
$CountryCode = read-host "Enter the country code"
}
if ($ZipCode -eq "" ) {
$ZipCode = read-host "Enter the zip code"
}
try {
write-progress "Reading zip-codes.csv..."
$PathToData=(get-item $MyInvocation.MyCommand.Path).directory
$PathToData="$PathToData/../Data"
$Table = import-csv "$PathToData/zip-codes.csv"
$FoundOne = 0
foreach($Row in $Table) {
if ($Row.country -eq $CountryCode) {
if ($Row.postal_code -eq $ZipCode) {
$City = $Row.city
$Lat = $Row.latitude
$Lon = $Row.longitude
write-host "* $City is at $Lat°N, $Lon°W"
$FoundOne = 1
}
}
}
if ($FoundOne) {
exit 0
}
write-error "Zip-code $ZipCode in country $CountryCode not found"
exit 1
} catch {
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}