New-TFSProject.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 26-Apr-2016.

<#
.SYNOPSIS
    Create new TFS project
.EXAMPLE
    New-TFSProject -Name Test -Description 'Test project' -ProcessTemplate Scrum
 
    Create a new TFS team project with given name and description and use Scrum process template.
#>

function New-TFSProject {
    [CmdletBinding()]
    param(
        #Name for the project
        [string] $Name,
        #Description for the project
        [string] $Description,
        #Version control type for the project
        [ValidateSet( 'Git', 'Tfvc')]
        [string] $SourceControlType='Git',
        #Software development schema for the project
        [string] $ProcessTemplate = 'Agile'
    )
    check_credential

    $uri = "$collection_uri/_apis/projects/?api-version=" + $global:tfs.api_version
    Write-Verbose "URI: $uri"

    $templateId = Get-TFSProcesses | ? name -eq $ProcessTemplate | % id
    if (!$templateId) { throw "No template exists with name: '$ProcessTemplate'" }
    Write-Verbose "Template id for '$ProcessTemplate': $templateId"

    $body = @{
        name = $Name
        description = $Description
        capabilities = @{
            processTemplate = @{ templateTypeId    = $templateId }
            versioncontrol  = @{ sourceControlType = $SourceControlType }
        }
    }

    $body = $body | ConvertTo-Json
    $params = @{ Uri = $uri; Method = 'Post'; Body = $body; ContentType = 'application/json' }
    invoke_rest $params
}