TrelloVoteCount.psm1

function Get-TrelloVoteCount
{
    <#
    .SYNOPSIS
        Displays tallied votes for one Trello board in either a GridView or DataTable.
 
    .DESCRIPTION
        Displays tallied votes for one Trello board in either a GridView or DataTable,
        ordered by number of votes desc. Columns listed are list, card, and votes.
 
    .PARAMETER Uri
        The Trello board URL. Use Uri or Id.
        Example: https://trello.com/b/NEerYXUU/powershell-sql-client-tools-sqlps-ssms
     
    .PARAMETER Id
        The Trello board ID. This can be found in the URL of the board after /b.
        Example: NEerYXUU
     
    .PARAMETER List
        Auto-populated parameter filled with list names from the speified board. Multiple
        lists are allowed.
     
    .PARAMETER ExcludeList
        Auto-populated parameter filled with list names from the speified board. Multiple
        excluded lists are allowed.
     
    .PARAMETER OutDataTable
        Return a datatable instead of output and Out-GridView. This is useful if you want to
        combine the results of multiple boards.
 
    .EXAMPLE
        Get-TrelloVoteCount -Uri https://trello.com/b/NEerYXUU/powershell-sql-client-tools-sqlps-ssms
         
        This shows the vote count for the public board at the URL listed above in a GridView, ordered by
        the number of votes per card.
 
    .EXAMPLE
        Get-TrelloVoteCount -Id NEerYXUU -ExcludeList Info
         
        This shows the vote count for the public board with the specified ID, but does not display
        the votes for list named "Info". Output is a GridView, ordered by the number of votes per card.
     
    .EXAMPLE
        Get-TrelloVoteCount -Id NEerYXUU -List Databases, 'Availability Groups' -OutDataTable
         
        This shows the vote count for the public board with the specified ID, and only displays the votes
        for the "Database" and "Availability Groups" lists. Output is a dadtatable, ordered by the number
        of votes per card.
 
    .OUTPUTS
        GridView or DataTable
 
    .NOTES
        Author: Chrissy LeMaire (@cl), netnerds.net
        Info: This module was made to help with our own Trello boards and Connect Items,
                so there's slight hidden customization that you'll see in the code.
 
#>

    [CmdletBinding(DefaultParameterSetName = 'All')]
    param (
        [Parameter(ParameterSetName = 'uri', Mandatory = $true)]
        [string]$Uri,
        [Parameter(ParameterSetName = 'id', Mandatory=$true)]
        [string]$Id,
        [switch]$OutDataTable,
        [Parameter(DontShow)]
        [switch]$ListLabels
    )
    
    DynamicParam
    {
        if ($id.length    -gt 0 -or $uri.length -gt 0)
        {
            if ($Uri.length    -gt 0)
            {
                $Id = $uri.Split("/")[4]
            }
            
            try
            {
                $lists = Invoke-RestMethod "https://api.trello.com/1/boards/$id/lists" -TimeoutSec 2
            }
            catch
            {
                $newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
                $list = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("List", [String[]],$null)
                $newparams.Add("List", $list)
                return $newparams
            }
            
            # Reusable parameter setup
            $newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $attributes = New-Object System.Management.Automation.ParameterAttribute
            $attributes.ParameterSetName = "__AllParameterSets"
            $attributes.Mandatory = $false
            $attributes.Position = 2
            $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)
            
            if ($lists)
            {
                $validationset = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $lists.name
                $attributeCollection.Add($validationset)
            }
            
            $List = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("List", [String[]], $attributeCollection)
            $ExcludeList = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ExcludeList", [String[]], $attributeCollection)
            $newparams.Add("List", $List)
            $newparams.Add("ExcludeList", $ExcludeList)
            return $newparams
        }
    }
    
    process
    {
        if ($Uri.length    -gt 0)
        {
            $Id = $uri.Split("/")[4]
        }
        
        if ($OutDataTable -eq $false) { Write-Output "Getting lists" }
        try
        {
            $lists = Invoke-RestMethod "https://api.trello.com/1/boards/$id/lists"
        }
        catch
        {
            throw "Board does not exist or you do not have permission to access it. Currently, only public boards are supported."
        }
        
        if ($OutDataTable -eq $false) { Write-Output "Filtering lists" }
        $list = $PSBoundParameters.List
        $exclude = $PSBoundParameters.ExcludeList
        if ($list.count -eq 0)
        {
            $lists = $lists.Where{ $_.closed -eq $false -and $_.name -notin $exclude }
        }
        else
        {
            $lists = $lists.Where{ $_.name -in $list }
        }
        
        if ($OutDataTable -eq $false) { Write-Output "Getting cards. This may take a couple seconds." }
        $cards = Invoke-RestMethod "https://api.trello.com/1/boards/$id/cards"
        $cards = $cards.Where{ $_.closed -eq $false -and $_.name -notin $exclude }
        
        $listcount = $lists.count
        $cardcount = $cards.count
        
        $board = Invoke-RestMethod "https://api.trello.com/1/boards/$id"
        $boardname = $board.name
        
        $datatable = New-Object System.Data.Datatable
        $datatable.TableName = "Vote Tally for $boardname"
        [void]$datatable.Columns.Add("List")
        [void]$datatable.Columns.Add("Card")
        [void]$datatable.Columns.Add("Votes", [int])
        
        if ($ListLabels) {
            [void]$datatable.Columns.Add("Connect")
        }
        
        foreach ($list in $lists)
        {
            $listcards = $cards.Where{ $_.idList -eq $list.id }
            
            foreach ($card in $listcards)
            {
                if ($card.badges.votes -gt 0)
                {
                    $row = $list.name, $card.name, $card.badges.votes
                    
                    if ($ListLabels) {
                        $filed = $null
                        if ($card.Labels.color -eq "blue") {
                            $filed = "Filed"
                        }
                        $row += $filed
                    }
                    [void]$datatable.Rows.Add($row)
                }
            }
        }
        
        if ($datatable.Rows.Count -gt 0) {
            $datatable.DefaultView.Sort = "votes DESC"
            $datatable = $datatable.DefaultView.ToTable()
            $totalsum = $($datatable.votes | Measure-Object -Sum).Sum
            if ($OutDataTable -eq $false)
            {
                $items = $datatable | Out-GridView -Title "Vote Tally: $boardname - $totalsum Total Votes" -PassThru
                foreach ($item in $items) {
                    $list = $lists.Where{ $_.name -eq $item.list }
                    $card = $cards.Where{ $_.name -eq $item.card -and $_.idlist -eq $list.id }
                    Start-Process $card.shortUrl
                }
            }
            else
            {
                return $datatable
            }
        }
        else
        {
            Write-Output "No votes to display :("
        }
    }
}



function Get-SsmsVote
{
    <#
    .SYNOPSIS
        Displays GridView of votes for the SSMS board at https://trello.com/b/M9NmFPfv
        If you use SQL Server Management Studio, come vote! Microsoft is listening.
    #>

    Get-TrelloVoteCount -Id 'M9NmFPfv' -ExcludeList 'Info' -ListLabels
}

function Get-SqlPsVote
{
    <#
    .SYNOPSIS
        Displays GridView of votes for the SQLPS board at https://trello.com/b/NEerYXUU
        If you use SQLPS, come vote! Microsoft is listening.
    #>

    
    Get-TrelloVoteCount -Id 'NEerYXUU' -ExcludeList 'Info' -ListLabels
}

Export-ModuleMember Get-TrelloVoteCount
Export-ModuleMember Get-SsmsVote
Export-ModuleMember Get-SqlPsVote