VisualStudioTeamServices.psm1

<#
    Name: VisualStudioTeamServices.psm1
    Description: Module file for VisualStudioTeamServices
    Author: Christopher Mank
    Date: 12/07/2015
#>


<#
.SYNOPSIS
    Gets Work Items from VSTS
  
.DESCRIPTION
    This cmdlet takes in connection info for your VSTS account and a query to access
    work items. These work items are then returned.
 
.PARAMETER StrAccessToken
    The access token generated in VSTS to grant access to the account
     
.PARAMETER StrVstsAccountName
    The account name for your VSTS account. This is the section that comes before the
    ".visualstudio.com" portion of your account URL.
     
.PARAMETER StrQuerySelectNoSpaces
    The SELECT clause of the work item query
     
.PARAMETER StrQueryWhere
    The WHERE clause of the work item query
     
.EXAMPLE
    Get-VstsWorkItemByQuery
  
.NOTES
    AUTHOR: Christopher Mank
    LASTEDIT: December, 07, 2015
#>

Function Get-VstsWorkItemByQuery
{
    [OutputType([System.Object])]

    Param
    (
        [Parameter(Mandatory=$True)]
        [String]$StrAccessToken,
        
        [Parameter(Mandatory=$True)]
        [String]$StrVstsAccountName,
        
        [Parameter(Mandatory=$True)]
        [String]$StrQuerySelectNoSpaces,

        [Parameter(Mandatory=$True)]
        [String]$StrQueryWhere
    )  
        
    # Configure the authentication header and account info
    $HshHeader = @{Authorization=("Basic {0}" -f $StrAccessToken)}
    $StrVstsAccountUrl = "https://$StrVstsAccountName.visualstudio.com/DefaultCollection/_apis"

    # Build the query from the query parts
    $StrQuery = "{`"query`": `"Select $StrQuerySelectNoSpaces From WorkItems Where $StrQueryWhere`"}"

    # Query Vsts for Work Items
    $JsnResponse = Invoke-RestMethod -Uri "$StrVstsAccountUrl/wit/wiql?api-version=1.0" -Headers $HshHeader -Method Post -ContentType application/json -Body $StrQuery

    # Return function if we didn't find any results
    If ($JsnResponse.workItems.Count -eq 0)
    {
        Return
    }

    # Loop through returned Work Items to build CSV list
    Foreach ($objWorkItem In $JsnResponse.workItems)
    {
        $StrIdList += $objWorkItem.id.ToString() + ","
    }
    $StrIdList = $StrIdList.TrimEnd(",")

    # Get Work Item data from Vsts
    $JsnResponse = Invoke-RestMethod -Uri "$StrVstsAccountUrl/wit/WorkItems?ids=$StrIdList&fields=$StrQuerySelectNoSpaces&api-version=1.0" -Headers $HshHeader -Method Get

    # Write Output
    Write-Output $JsnResponse.value
}

<#
.SYNOPSIS
    Creates Work Items in VSTS
  
.DESCRIPTION
    This cmdlet takes in connection info for your VSTS account and a project name and
    work item title. It then creates the work item based on the type passed.
 
.PARAMETER StrAccessToken
    The access token generated in VSTS to grant access to the account
     
.PARAMETER StrVstsAccountName
    The account name for your VSTS account. This is the section that comes before the
    ".visualstudio.com" portion of your account URL.
     
.PARAMETER StrProjectName
    The name of the VSTS project
     
.PARAMETER StrWiTypeName
    The type of work item to create (Backlog Item, Bug, etc.)
     
.PARAMETER StrTitle
    The title of the work item
     
.EXAMPLE
    New-VstsWorkItem
  
.NOTES
    AUTHOR: Christopher Mank
    LASTEDIT: December, 07, 2015
#>

Function New-VstsWorkItem 
{
    [OutputType([System.Object])]

    Param
    (    
        [Parameter(Mandatory=$True)]
        [String]$StrAccessToken,
        
        [Parameter(Mandatory=$True)]
        [String]$StrVstsAccountName,
        
        [Parameter(Mandatory=$True)]
        [String]$StrProjectName,

        [Parameter(Mandatory=$True)]
        [String]$StrWiTypeName,

        [Parameter(Mandatory=$True)]
        [String]$StrTitle
    )  
        
    # Configure the basic authentication header and account info
    $HshHeader = @{Authorization=("Basic {0}" -f $StrAccessToken)}
    $StrVstsAccountUrl = "https://$StrVstsAccountName.visualstudio.com/DefaultCollection/$StrProjectName/_apis"

    # Build request body
    $JsnBody = "[`
        {`
            `"op`": `"add`",
            `"path`": `"/fields/System.Title`",
            `"value`": `"$StrTitle`"
        }
    ]"


    # Create new Vsts Work Item
    $StrUri = "$StrVstsAccountUrl/wit/workitems/`$$StrWiTypeName" + "?api-version=1.0"
    $JsnResponse = Invoke-RestMethod -Uri $StrUri -Headers $HshHeader -Method Patch -ContentType application/json-patch+json -Body $JsnBody

    # Write Output
    Write-Output $JsnResponse
}

<#
.SYNOPSIS
    Updates Work Items in VSTS
  
.DESCRIPTION
    This cmdlet takes in connection info for your VSTS account and a work item Id
    and updates the title with the information passed.
 
.PARAMETER StrAccessToken
    The access token generated in VSTS to grant access to the account
     
.PARAMETER StrVstsAccountName
    The account name for your VSTS account. This is the section that comes before the
    ".visualstudio.com" portion of your account URL.
     
.PARAMETER IntWorkItemId
    The Work Item Id to update
     
.PARAMETER StrTitle
    The title of the work item
     
.EXAMPLE
    Update-VstsWorkItem
  
.NOTES
    AUTHOR: Christopher Mank
    LASTEDIT: December, 07, 2015
#>

Function Update-VstsWorkItem
{
    [OutputType([System.Object])]

    Param
    (    
        [Parameter(Mandatory=$True)]
        [String]$StrAccessToken,
        
        [Parameter(Mandatory=$True)]
        [String]$StrVstsAccountName,
        
        [Parameter(Mandatory=$True)]
        [String]$IntWorkItemId,       

        [Parameter(Mandatory=$True)]
        [String]$StrTitle
    )  
        
    # Configure the basic authentication header and account info
    $HshHeader = @{Authorization=("Basic {0}" -f $StrAccessToken)}
    $StrVstsAccountUrl = "https://$StrVstsAccountName.visualstudio.com/DefaultCollection/_apis"

    # Build request body
    $JsnBody = "[`
        {`
            `"op`": `"add`",
            `"path`": `"/fields/System.Title`",
            `"value`": `"$StrTitle`"
        }
    ]"


    # Update Vsts Work Item
    $StrUri = "$StrVstsAccountUrl/wit/workitems/$IntWorkItemId" + "?api-version=1.0"
    $JsnResponse = Invoke-RestMethod -Uri $StrUri -Headers $HshHeader -Method Patch -ContentType application/json-patch+json -Body $JsnBody

    # Write Output
    Write-Output $JsnResponse
}