public/Get-FootballFixtureCommentary.ps1

function Get-FootballFixtureCommentary {

    [CmdletBinding()]
    param(

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

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

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

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

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

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {

            $FixtureFolderName = Get-FootballFixtureFolderName -Competition $Competition -Date $Date -Path $Path
            $FxitureResults = Get-ChildItem -Path "$Path\sportsmonk-results\$Competition\$Date"

            $Hash1 =@{
                Competition = $Competition
                Date = $Date
                FixtureFolderName = $($FixtureFolderName.Count)
                FixtureResults = $($FxitureResults.Count)
                PSFunctionName = $($MyInvocation.MyCommand.Name)
            }

            $Hash1 | Format-Table

            if ($($FxitureResults.Count) -ge 1) {

                $ReqiredFixtures = Import-Csv -Path $($FxitureResults.FullName)
        
                foreach ($FolderName in $FixtureFolderName) {

                    $FixtureIdToUse = $FolderName.split('-')[0]
                    $ReqiredFixtures = Import-Csv -Path $($FxitureResults.FullName) | Where-Object {$_.Fixture_Id -eq $FixtureIdToUse}

                    foreach ($Fixture in $ReqiredFixtures) { 

                        $CommentaryFiles = Get-ChildItem -Path "$Path\fixture-artifact\post-match-news\$Competition\$Date\$FolderName" | Where-Object {$_.Name -like '*commentary.csv'}
                        $CommentaryFilePath = "$Path\fixture-artifact\post-match-news\$Competition\$Date\$FolderName\commentary.csv"

                        if ($Force) {

                            $CommentaryFiles | Remove-Item -Force -Verbose

                        } # if

                        if (!$Force -and $($CommentaryFiles.Count) -gt 0) {

                            Write-Warning -Message "File exists: $($CommentaryFiles.Count)"
                            continue

                        } # if

                        $UriToUse = "https://api.sportmonks.com/v3/football/commentaries/fixtures/$($Fixture.Fixture_Id)?api_token=$Token&include=player;relatedPlayer"
                        $Response = Invoke-RestMethod -Uri $UriToUse -Method 'GET' -Headers $Header
                        $ResponseData = $($Response.data)
                        [PSCustomObject]$NewObjects=@()

                        Foreach ($Data in $ResponseData) {

                            $NewObject = $Data.PSObject.Copy()
                            $Player = $($Data.player)
                            $RelatedPlayer = $($Data.relatedplayer)

                            if ($($Player.Count) -ge 1) {

                                $NewObject | Add-Member -MemberType NoteProperty -Name playerInvolved -Value $($Player.display_name)

                            }
                            else {

                                $NewObject | Add-Member -MemberType NoteProperty -Name playerInvolved -Value none

                            } # if

                            if ($($RelatedPlayer.Count) -ge 1) {
                                
                                $NewObject | Add-Member -MemberType NoteProperty -Name relatedPlayerInvolved -Value $($RelatedPlayer.display_name)
                        
                            }
                            else {

                                $NewObject | Add-Member -MemberType NoteProperty -Name relatedPlayerInvolved -Value none
                                
                            } # if

                            $NewObjects += $NewObject

                        } # foreach

                        $NewObjects  | Select-Object -ExcludeProperty player,relatedPlayer | Export-Csv -Path $CommentaryFilePath -Force -Verbose

                    } # foreach

                } # foreach

            } # if

        }
        catch {

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

        } # trycatch
            
    } # process

} # function