Public/Get-P2000Entry.ps1

# .SYNOPSIS
# Retrieves P2000 emergency services notifications
function Get-P2000Entry
{
    [CmdletBinding(DefaultParameterSetName = 'All')]
    param
    (
        [Parameter(ParameterSetName = 'Province')]
        [ValidateSet('groningen', 'friesland', 'drenthe', 'overijssel', 'flevoland', 'gelderland', 'utrecht', 'noord-holland', 'zuid-holland', 'zeeland', 'noord-brabant', 'limburg')]
        [string]$Province,

        [Parameter(ParameterSetName = 'Region')]
        [ValidateScript( { (Get-P2000Region).ID -contains $_ } )]
        [string]$Region,

        [Parameter(ParameterSetName = 'Postcode')]
        [ValidatePattern('\d{4}')]
        [string]$PostCode
    )

    $Uri = switch ($PSCmdlet.ParameterSetName)
    {
        'Province'
        {
            "http://www.alarmfase1.nl/provincie/$Province/"
        }
        'Region'
        {
            "http://www.alarmfase1.nl/$Region/"
        }
        'Postcode'
        {
            "http://www.alarmfase1.nl/postcode/$PostCode/"
        }
        'All'
        {
            'http://www.alarmfase1.nl/'
        }
    }

    Invoke-WebRequest -Uri $Uri |
        Select-Object -ExpandProperty Content |
        pup '.call json{}' |
        ConvertFrom-Json |
        ForEach-Object { $_.GetEnumerator() } |
        ForEach-Object {
        $Properties = [Ordered]@{}
        $Properties.Latitude = $_.Latitude
        $Properties.Longitude = $_.Longitude
        $Properties.Service = $_.Service

        if ($_.Children[0])
        {
            if ($_.Children[0].Children[0])
            { 
                $Properties.DateTimeText = $_.Children[0].Children[0].content 
                try 
                {
                    $Properties.DateTime = [DateTime]::ParseExact($_.Children[0].Children[0].content + ':00', 's', $null)
                }
                catch
                {
                    
                }                
            }

            if ($_.Children[0].Children[1])
            {
                $Properties.Link = $_.Children[0].Children[1].href

                if ($_.Children[0].Children[1].Children[0])
                {
                    $Properties.Title = $_.Children[0].Children[1].Children[0].text
                }
            }
        }

        if ($_.Children[1])
        {
            $Properties.Original = $_.Children[1].text
        }

        if ($_.Children[2])
        {
            if ($_.Children[2].Children[1])
            {
                if ($_.Children[2].Children[1].Children[3])
                {
                    $Properties.Address = $_.Children[2].Children[1].Children[3].text
                }

                if ($_.Children[2].Children[1].Children[1])
                {
                    if ($_.Children[2].Children[1].Children[1].Children[0])
                    {
                        $Properties.City = $_.Children[2].Children[1].Children[1].Children[0].text
                    }
                }

                if ($_.Children[2].Children[1].Children[2])
                {
                    if ($_.Children[2].Children[1].Children[2] | Get-Member Children -MemberType NoteProperty)
                    {
                        $Properties.PostCode = $_.Children[2].Children[1].Children[2].Children[0].text
                    }
                }

                if ($_.Children[2].Children[1].Children[0])
                { 
                    $Properties.Province = ($_.Children[2].Children[1].Children[0].href -split '/')[4]
                }
            }
        }

        $Properties.PSTypeName = 'UncommonSense.P2000.Entry'
        [pscustomobject]$Properties
    }
}