2024-11-08 12:38:20 +01:00
|
|
|
The *locate-zip-code.ps1* Script
|
|
|
|
===========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script prints the geographic location of the given zip-code.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-11-08 12:35:11 +01:00
|
|
|
/home/markus/Repos/PowerShell/scripts/locate-zip-code.ps1 [[-CountryCode] <String>] [[-ZipCode] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-CountryCode <String>
|
|
|
|
Specifies the country code
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-ZipCode <String>
|
|
|
|
Specifies the zip code
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./locate-zip-code.ps1 de 87600
|
|
|
|
* DE 87600 Kaufbeuren is at 47.8824°N, 10.6219°W
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Prints the geo location of a zip-code
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script prints the geographic location of the given zip-code.
|
|
|
|
.PARAMETER CountryCode
|
|
|
|
Specifies the country code
|
|
|
|
.PARAMETER ZipCode
|
|
|
|
Specifies the zip code
|
|
|
|
.EXAMPLE
|
2023-08-06 21:36:33 +02:00
|
|
|
PS> ./locate-zip-code.ps1 de 87600
|
|
|
|
* DE 87600 Kaufbeuren is at 47.8824°N, 10.6219°W
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$CountryCode = "", [string]$ZipCode = "")
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($CountryCode -eq "" ) { $CountryCode = read-host "Enter the country code" }
|
|
|
|
if ($ZipCode -eq "" ) { $ZipCode = read-host "Enter the zip code" }
|
|
|
|
|
|
|
|
write-progress "Reading zip-codes.csv..."
|
2023-12-07 20:24:45 +01:00
|
|
|
$Table = import-csv "$PSScriptRoot/../data/zip-codes.csv"
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
$FoundOne = 0
|
|
|
|
foreach($Row in $Table) {
|
|
|
|
if ($Row.country -eq $CountryCode) {
|
|
|
|
if ($Row.postal_code -eq $ZipCode) {
|
|
|
|
$Country=$Row.country
|
|
|
|
$City = $Row.city
|
|
|
|
$Lat = $Row.latitude
|
|
|
|
$Lon = $Row.longitude
|
|
|
|
write-output "* $Country $ZipCode $City is at $Lat°N, $Lon°W"
|
|
|
|
$FoundOne = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($FoundOne) {
|
|
|
|
exit 0 # success
|
|
|
|
}
|
|
|
|
throw "Zip-code $ZipCode in country $CountryCode not found"
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-11-20 11:52:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:57)*
|