PowerShell/Docs/locate-city.md

91 lines
2.0 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*locate-city.ps1*
================
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
2023-07-29 10:15:44 +02:00
PS> ./locate-city.ps1 [[-City] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-City <String>
Specifies the city to look for
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
2023-08-06 21:36:33 +02:00
PS> ./locate-city.ps1 Paris
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.
.PARAMETER City
Specifies the city to look for
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./locate-city.ps1 Paris
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$City = "")
try {
2023-08-06 21:36:33 +02:00
if ($City -eq "" ) { $City = Read-Host "Enter the city name" }
2022-11-17 20:02:26 +01:00
2023-08-06 21:36:33 +02:00
Write-Progress "Reading worldcities.csv..."
2022-11-17 20:02:26 +01:00
$Table = import-csv "$PSScriptRoot/../Data/worldcities.csv"
$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"
}
}
if ($FoundOne) {
exit 0 # success
}
write-error "City $City not found"
exit 1
} 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
2023-09-20 17:05:11 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of locate-city.ps1 as of 09/20/2023 17:04:42)*