Public/Get-Weather.ps1
<# .EXAMPLE Get-Weather -Latitude 52.3 -Longitude 3.4 -Units metric -Language nl -ApiKey 279c1fbf493f93d8eaa9a56cf225a23d #> function Get-Weather { param ( [Parameter(Mandatory, Position = 0)] [ValidateRange(-90, 90)] [decimal]$Latitude, [Parameter(Mandatory, Position = 1)] [ValidateRange(-180, 180)] [decimal]$Longitude, [ValidateSet('standard', 'metric', 'imperial')] [string]$Units = 'standard', [ValidateSet('en', 'nl')] [string]$Language, [ValidateNotNull()] [string]$ApiKey, [switch]$Raw ) $Url = 'https://api.openweathermap.org/data/3.0/onecall?lat={0}&lon={1}&units={2}&lang={3}&appid={4}' -f $Latitude, $Longitude, $Units, $Language, $ApiKey Write-Verbose "Using url $Url" if ($Raw) { Invoke-WebRequest -Uri $Url | Select-Object -ExpandProperty Content } else { $Response = Invoke-RestMethod -Uri $Url $TemperatureUnit = switch ($Units) { 'standard' { 'K' } 'metric' { 'ºC' } 'imperial' { 'ºF' } } [PSCustomObject]@{ PSTypeName = 'UncommonSense.Weather' Latitude = $Response.lat Longitude = $Response.lon TemperatureUnit = $TemperatureUnit TimeZone = $Response.timezone TimeZoneOffset = [TimeSpan]::FromSeconds($Response.timeZone_offset) Current = $Response.current | Convert-Current Minutely = $Response.minutely | Convert-Minutely Hourly = $Response.hourly | Convert-Hourly Daily = $Response.daily | Convert-Daily Alerts = $Response.alerts ?? @() | Convert-Alert } } } |