2024-11-08 12:38:20 +01:00
|
|
|
The *locate-city.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 city.
|
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-city.ps1 [[-city] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
-city <String>
|
|
|
|
Specifies the name of the city to look for
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
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
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./locate-city.ps1 Amsterdam
|
|
|
|
* Amsterdam (United States, New York, population 21241) is at 42.9420°N, -74.1907°W
|
|
|
|
* Amsterdam (Netherlands, Noord-Holland, population 1031000) is at 52.3500°N, 4.9166°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 geographic location of a city
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script prints the geographic location of the given city.
|
2024-05-19 10:25:56 +02:00
|
|
|
.PARAMETER city
|
|
|
|
Specifies the name of the city to look for
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./locate-city.ps1 Amsterdam
|
|
|
|
* Amsterdam (United States, New York, population 21241) is at 42.9420°N, -74.1907°W
|
|
|
|
* Amsterdam (Netherlands, Noord-Holland, population 1031000) is at 52.3500°N, 4.9166°W
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
param([string]$city = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
try {
|
2024-05-19 10:25:56 +02:00
|
|
|
if ($city -eq "" ) { $city = Read-Host "Enter the name of the city" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
Write-Progress "Reading data/worldcities.csv..."
|
|
|
|
$table = Import-CSV "$PSScriptRoot/../data/worldcities.csv"
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
$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
|
|
|
|
Write-Host "* $city ($country, $region, population $population) is at $lat°N, $long°W"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-05-19 10:25:56 +02:00
|
|
|
if (-not $foundOne) { throw "No city '$city' found in database" }
|
|
|
|
exit 0 # success
|
2022-11-17 20:02:26 +01:00
|
|
|
} catch {
|
2024-05-19 10:25:56 +02:00
|
|
|
"⚠️ Error $($_.InvocationInfo.ScriptLineNumber): $($Error[0])."
|
2022-11-17 20:02:26 +01:00
|
|
|
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)*
|