PowerShell/Docs/locate-city.md
2022-12-04 10:40:18 +01:00

85 lines
1.9 KiB
Markdown

## The *locate-city.ps1* Script
This PowerShell script prints the geographic location of the given city.
## Parameters
```powershell
locate-city.ps1 [[-City] <String>] [<CommonParameters>]
-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.
```
## Example
```powershell
PS> ./locate-city Paris
```
## Notes
Author: Markus Fleschutz | License: CC0
## Related Links
https://github.com/fleschutz/PowerShell
## Source Code
```powershell
<#
.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
PS> ./locate-city Paris
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$City = "")
try {
if ($City -eq "" ) { $City = read-host "Enter the city name" }
write-progress "Reading worldcities.csv..."
$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
}
```
*Generated by convert-ps2md.ps1 using the comment-based help of locate-city.ps1*