Accessing the FRC API using PowerShell

Normally I work in C#, and I have some impressively complex classes devised to work with the FRC API, but for a presentation recently I wanted something that was more accessible to demonstrate access. So I developed a couple of PowerShell scripts which will run on any Windows machine with an updated version of PowerShell installed.

So without further ado, this bit of script asks for you FRC authorization key, then stores the complete match detail for the 2017 NorthStar regional in Minneapolis to a CSV file stored in the C:\FRCFiles directory.

Obviously if you don’t have an API key this won’t work.

$APICredential = Get-Credential  -Message "Enter you API Authorization key"
$year = "2017" #2015 and on should be supported
$eventCode = "MNMI2"
$targetDirectory = "C:\FRCFiles"

#Between the Schedule and the Scores endpoint all match data is available.
$qualMatches = Invoke-RestMethod -Uri HTTPS://frc-api.firstinspires.org/v2.0/$year/schedule/$eventcode/qual/hybrid -Credential $APICredential
$playoffMatches = Invoke-RestMethod -Uri HTTPS://frc-api.firstinspires.org/v2.0/$year/schedule/$eventcode/playoff/hybrid -Credential $APICredential

$qualScores = Invoke-RestMethod -Uri HTTPS://frc-api.firstinspires.org/v2.0/$year/scores/$eventcode/qual -Credential $APICredential
$playoffScores = Invoke-RestMethod -Uri HTTPS://frc-api.firstinspires.org/v2.0/$year/scores/$eventcode/playoff -Credential $APICredential

$allMatches = $qualMatches.Schedule + $playoffMatches.Schedule
$allScores = $qualScores.MatchScores + $PlayoffScores.MatchScores

#matchScores is list of flattened match data items
$matchscores = [Pscustomobject]@()

foreach ( $match in $allScores) #iterate through scores because a score guarantees a match but not the reverse
{
    #Because the JSON objects returned contain hierarchical data we need to go in and flatten it.
    $matchDetail = New-Object PSObject
    Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name year -Value $year
    Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name eventCode -Value $eventCode

    $allMatches | Where-Object  {$_.matchNumber -eq $match.matchNumber -and $_.tournamentLevel -eq $match.matchLevel} -OutVariable targetMatch | Out-Null

    foreach ($Property in $targetMatch[0].psobject.Properties)
    {
        if ($Property.Name -ne "Teams")
        {
            Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $Property.Name -Value $Property.Value
        }
    }
    foreach ($team in $targetMatch[0].Teams)
    {
        $name = $team.station + "TeamNumber"
        Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $name -Value $team.teamNumber
        $name = $team.station + "Surrogate"
        Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $name -Value $team.surrogate
        $name = $team.station + "DQ"
        Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $name -Value $team.dq

    }


    foreach ( $Property in $match.psobject.Properties)
    {
        if ($Property.Name -ne "Alliances")
        {
            Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $Property.Name -Value $Property.Value
        }
    }
    foreach ($alliance in $match.Alliances)
    {
        $alliancecolor = $alliance.alliance
        foreach ( $Property in $alliance.psobject.Properties)
        {
            if ($Property.Name -ne "alliance")
            {
                $cname = $alliancecolor + $Property.Name
                Add-Member -InputObject $matchDetail -MemberType NoteProperty -Name $cname -Value $Property.Value
            }
        }
    }
    $matchscores += $matchDetail
}
$matchscores | select * | ConvertTo-Csv -NoTypeInformation -delimiter "`t"  > $targetDirectory\$eventCode-$year.csv

For those of you who don’t have API access I’ve attached the CSV generated from last year’s NorthStar Regional.

MNMI2-2017.csv (147 KB)