public/Get-FixtureResult.ps1

function Get-FixtureResult {
    <#
        .SYNOPSIS
            Retrieves the predicted result of a fixture based on provided data.
 
        .EXAMPLE
            Get-FixtureResult -FixtureList $AllFuxtures -PredictionObject $Predictions
 
        .INPUTS
            - [Object] FixtureList
            - [Object] PredictionObject
 
    #>

    [CmdletBinding()]
    param(

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

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

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

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

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {
            
            $NewObjects =@()
            $UnqiueObjects = $PredictionObject | Select-Object -Property fixture_id -Unique
            Write-Warning -Message "$($MyInvocation.MyCommand.Name): Professing fixture results. Unique prediction objects $($UnqiueObjects.Count)."

            foreach ($Object in $UnqiueObjects) {
                
                $Match = ''
                $Match = $FixtureList | Where-Object {$_.id -eq $($Object.Fixture_Id)} | Select-Object -First 1
                Write-Warning -Message "Processing fixture result for $($Object.Fixture_Id)."

                if ($Match) {

                    Write-Warning -Message "Processing fixture result for $($Match.Id) - $($Match.name)."
                    $Uri = "https://api.sportmonks.com/v3/football/fixtures/$($Object.Fixture_Id)?api_token=$Token&include=statistics"
                    $Response = Invoke-RestMethod $Uri -Method 'GET' -Headers $Header
                    $FullTimeResult = $response.data.statistics | Where-Object {$_.type_id -eq 52}
                    $HomeScore = $FullTimeResult | Where-Object {$_.location -eq 'home'}
                    $AwayScore = $FullTimeResult | Where-Object {$_.location -eq 'away'}

                    $NewObject = $Object.PSObject.Copy()
                    $NewObject | Add-Member -MemberType NoteProperty -Name FixtureName -Value $($Match.name)
                    $NewObject | Add-Member -MemberType NoteProperty -Name MatchResult -Value $($Match.result_info)
                    $NewObject | Add-Member -MemberType NoteProperty -Name HomeScore -Value $($HomeScore.data.value)
                    $NewObject | Add-Member -MemberType NoteProperty -Name AwayScore -Value $($AwayScore.data.value)
                    $NewObject.psobject.properties.remove('TypeName')
                    $NewObject.psobject.properties.remove('TypeId')
                    $NewObjects += $NewObject

                }
                else {

                    Write-Warning -Message "No match for fixture $($Object.Fixture_Id)."
                } # if
                
            } # foreach
            
            return $NewObjects
        }
        catch {

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

        } # trycatch

    } # process

} # function