Additions/Create-AdditionDocumentation.ps1

function Create-AdditionDocumentation
{
    Param(
    [Parameter(Mandatory=$true)]
    [string]$BranchPath,
    [Parameter(Mandatory=$false)]
    [string]$ExistingFilePath
    )

    $additionName = $BranchPath.SubString($BranchPath.IndexOf("/") + 1, $BranchPath.LastIndexOf("/") - $BranchPath.IndexOf("/") - 1)
    $WorkItemVersionListNos = @()
    $WorkItemTitles = @()
    $WorkItemDescriptions = @()
    $Pattern = '[a-zA-Z]'
    $UpdateExistingFile = $false
    [string]$ChangesetProgressName = "Reading changesets for {0} ..." -f $additionName
    [string]$ReadProgressName = "Reading workitems for changesets in {0}..." -f $additionName
    [string]$WriteProgressName = "Writing workitems for {0} ..." -f $additionName

    #Get changesets for the branch
    $url = '_apis/tfvc/changesets/?searchCriteria.itemPath={0}' -f $BranchPath
    $changeSets = Invoke-TFSAPI -Url $url

    #Load existing md file
    if ([System.IO.File]::Exists($ExistingFilePath))
    {
        if ([IO.Path]::GetExtension($ExistingFilePath) -ne '.md')
        {
            Write-Error 'Incorrect file extension. Documentation file must be of type Markdown (.md)'
        }
        else
        {
            #Get the latest version of the file
            $UpdateExistingFile = $true
            $CurrentExistingFile = Get-Content($ExistingFilePath)
            $ExistingFileVersion = get-content $ExistingFilePath -Head 1
            $ExistingFileVersion = $ExistingFileVersion -replace $Pattern, ''
            $ExistingFileVersion = $ExistingFileVersion.Trim()
        }
    }
    $CurrChangeset = 0
    Write-Host 'Finding workitems for' $additionName -ForegroundColor Green
    foreach ($changeSet in $changeSets.value)
    {        
        $CurrChangeset++
        Write-Progress $ChangesetProgressName -Status 'Progress' -PercentComplete (($CurrChangeset / $changeSets.count)*100) -Id 1

        $changeSetUrl = $changeSet.url
        $changeSetDetails = Invoke-TFSAPI -Url $changeSetUrl
        #Go through list of workitems for the changeset
        $CurrWorkItem = 0
        foreach ($WorkItemsUrl in $changeSetDetails._links.workItems.href)
        { 
            $WorkItems = Invoke-TFSAPI -Url $WorkItemsUrl
            
            foreach ($WorkItem in $WorkItems)
            {
                #Retrieve details for each work item
                foreach ($WorkItemValue in $WorkItem.value)
                {
                    if ($WorkItemValue.workItemType -ne "Code Review Request")
                    {           
                        $CurrWorkItem++
                        Write-Progress $ReadProgressName -Status 'Progress' -PercentComplete (($CurrWorkItem / $WorkItem.count)*100) -ParentId 1

                        $WorkItemId = $WorkItemValue.id
                        $WorkItemUrl = '_apis/wit/workitems/{0}' -f $WorkItemId
                        $DetailedWorkItem = Invoke-TFSAPI -Url $WorkItemUrl
                        $FullWorkItemVersion = $DetailedWorkItem.fields.'TechnologyManagement.TMScrum.VersionList'
                        $WorkItemVersion = $FullWorkItemVersion -replace $Pattern, ''
                        $WorkItemVersion = $WorkItemVersion.Trim()
                        if ($UpdateExistingFile)
                        {
                            if ($WorkItemVersion -ne '' -and $ExistingFileVersion -ne '')
                            {
                                if ([System.Version]$WorkItemVersion -gt [System.Version]$ExistingFileVersion)
                                {
                                    $WorkItemVersionListNos += $FullWorkItemVersion  
                                    $WorkItemTitles += $WorkItemValue.title                        
                                    $WorkItemDescriptions += Get-WorkItemDescription -WorkItemNo $WorkItemId -ReturnTitle $false -IncludeChildren $false
                                }
                            }
                        }
                        else
                        {
                            $WorkItemVersionListNos += $FullWorkItemVersion  
                            $WorkItemTitles += $WorkItemValue.title                        
                            $WorkItemDescriptions += Get-WorkItemDescription -WorkItemNo $WorkItemId -ReturnTitle $false -IncludeChildren $false
                        }
                    }
                }
            }
        }
    }
    Write-Host 'Building file content for markdown' -ForegroundColor Green
    $index = 0
    $WorkItemCount = $WorkItemTitles.Count
    #Build the contents for the md file
    if ($WorkItemCount -gt 0)
    {    
        Do 
        {
            Write-Progress $WriteProgressName -Status "$i% Complete:" -PercentComplete $index
            $Version = $WorkItemVersionListNos[$index]
            $Title = $WorkItemTitles[$index]
            $Description = $WorkItemDescriptions[$index]
            $FileContent += "`r`n# {0}`r`n## {1}`r`n{2}`r`n" -f $Version, $Title, $Description
            $index++   
        } 
        until( $index -eq $WorkItemCount)
    }   

    if ($ExistingFilePath -eq '')
    {
        $filename = "{0}\{1}.md" -f [Environment]::GetFolderPath("Desktop"), $additionName
    }
    else
    {
        $filename = $ExistingFilePath
    }

    if ([System.IO.File]::Exists($filename))
    {
        [System.IO.File]::Delete($filename)
    }

    #write the content to the filename defined above
    if ($FileContent -eq $null)
    {
        Write-Host 'Existing file is up to date with changes' -ForegroundColor Green
    }
    else
    {
        Add-Content -Path $filename -Value $FileContent
    }

    if ($UpdateExistingFile) 
    {
        Add-Content -Path $filename -Value $CurrentExistingFile
    }
    Write-Host 'Complete' -ForegroundColor Green
}

Export-ModuleMember -Function Create-AdditionDocumentation