public/Get-FixtureListPrediction.ps1

function Get-FixtureListPrediction {

    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Competition,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [Object]$FixtureList,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [Object]$FixtureListPrediction,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [Object]$Header,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int]$Type

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {

            # Filter the fixtures for the league.
            $Identifier = Get-LeagueIdentifier -Competition $Competition
            $ReqiredFixtures = $FixtureList | Where-Object {$_.league_id -eq $Identifier}

            # Get the type information.
            $TypeUri = 'https://api.sportmonks.com/v3/core/types/'+$Type+'?api_token='+$Token
            $TypeResponse = Invoke-RestMethod $TypeUri -Method 'GET' -Headers $Headers

            # Array object to return.
            $PredictionObjects = @()

            foreach ($Fixture in $ReqiredFixtures) {

                #Write-Warning -Message "Porcessing $Type."
                #$Fixture | Format-Table

                Write-Warning -Message "Type is: $Type."
                Write-Warning -Message "Fixture Id: $($Fixture.id)."
                Write-Warning -Message "Fixture Id: $($Fixture.name)."

                if ($($PSBoundParameters.ContainsKey('FixtureListPrediction'))) {

                    $FixturePrediction = $FixtureListPrediction | Where-Object {$_.fixture_id -eq $($Fixture.id) -and $_.type_id -eq $Type}

                }
                else {

                    # Fetch predictions for the fixture
                    $Response = Invoke-RestMethod -Uri "https://api.sportmonks.com/v3/football/predictions/probabilities/fixtures/$($Fixture.id)?api_token=$Token" -Method 'GET' -Headers $Header
                    $FixturePrediction = $($Response.data) | Where-Object {$_.fixture_id -eq $($Fixture.id) -and $_.type_id -eq $Type}

                } # if

                if ($($FixturePrediction.Count) -lt 1) {

                    Write-Warning -Message "FixtureFullTimePrediction Count: $($FixturePrediction.Count)."
                    continue

                }

                if ($Type -eq 231 -or $Type -eq 234 -or $Type -eq 235 -or $Type -eq 236) {

                    $PredictionObject = [PSCustomObject]@{
                        FixtureId   = $($Fixture.id)
                        FixtureName = $($Fixture.name)
                        leagueId    = $($Fixture.league_id)
                        StartingAt  = $($Fixture.starting_at)
                        TypeId      = $($TypeResponse.data.id)
                        TypeName    = $($TypeResponse.data.name)
                        Yes         = $($FixturePrediction.predictions.yes)
                        No          = $($FixturePrediction.predictions.no)
                    }

                    # Add prediction object to the collection
                    $PredictionObjects += $PredictionObject

                }

                if ($Type -eq 233 -or $Type -eq 237 -or $Type -eq 238) {

                    # Create prediction object
                    $PredictionObject = [PSCustomObject]@{
                        FixtureId   = $($Fixture.id)
                        FixtureName = $($Fixture.name)
                        leagueId    = $($Fixture.league_id)
                        StartingAt  = $($Fixture.starting_at)
                        TypeId      = $($TypeResponse.data.id)
                        TypeName    = $($TypeResponse.data.name)
                        Home        = $($FixturePrediction.predictions.home)
                        Away        = $($FixturePrediction.predictions.away)
                        Draw        = $($FixturePrediction.predictions.draw)
                    }

                    # Add prediction object to the collection
                    $PredictionObjects += $PredictionObject

                } # if

                if ($Type -eq 239) {

                    # Create prediction object
                    $PredictionObject = [PSCustomObject]@{
                        FixtureId    = $($Fixture.id)
                        FixtureName  = $($Fixture.name)
                        leagueId     = $($Fixture.league_id)
                        StartingAt   = $($Fixture.starting_at)
                        TypeId       = $($TypeResponse.data.id)
                        TypeName     = $($TypeResponse.data.name)
                        DrawHome     = $($FixturePrediction.predictions.draw_home)
                        DrawAway     = $($FixturePrediction.predictions.draw_away)
                        HomeAway     = $($FixturePrediction.predictions.home_away)
                    }

                    $PredictionObjects += $PredictionObject

                }

                if ($Type -eq 232 -or $Type -eq 240) {

                    # Create prediction object
                    $PredictionObject = [PSCustomObject]@{
                        FixtureId    = $($Fixture.id)
                        FixtureName  = $($Fixture.name)
                        leagueId     = $($Fixture.league_id)
                        StartingAt   = $($Fixture.starting_at)
                        TypeId       = $($TypeResponse.data.id)
                        TypeName     = $($TypeResponse.data.name)
                    }

                    if ($Type -eq 240) {

                        $($FixturePrediction.predictions.scores.PSObject.Properties) | ForEach-Object -Process {

                            $PredictionObject | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value -Force
                        
                        }

                    }
                    else {

                        $($FixturePrediction.predictions.PSObject.Properties) | ForEach-Object -Process {

                            $PredictionObject | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value -Force
                        
                        }

                    }

                    $PredictionObjects += $PredictionObject

                }

            } # foreach

            return $PredictionObjects

        }
        catch {

            "$($MyInvocation.MyCommand.Name): $_.Exception.Message"

        } # trycatch

    } # process

} # function