Modules/Get-RestVsts.psm1

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
48
49
50
51
. "$PSScriptRoot\Test-Environment.ps1"
<#
.SYNOPSIS
Make a raw REST request against a VSTS instance
 
.DESCRIPTION
This is a pretty raw cmdlet and should probably have been left private, but maybe it helps someone.
 
.PARAMETER afterProject
Url fragment after the instance name
 
.PARAMETER version
Either 1.0 or 2.0 depending on the API endpoint
 
.EXAMPLE
# Get all repositories in the VSTS instance
$response = Get-RestVsts -U "_apis/git/repositories/" -V 1.0
 
.NOTES
General notes
#>

function Get-RestVsts {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)][Alias("U")]
        [string]$afterProject,
        [Parameter(Mandatory = $true)][Alias("V")]
        [string]$version,
        [Parameter(Mandatory = $false)][Alias("np")]
        [switch]
        [bool]$noProject
    )
    test-environment
    $v = Get-VstsConnection
    $url = "https://$($v.InstanceName).visualstudio.com/DefaultCollection" 
    if (-not $noProject) {
        $url += '/'+$v.ProjectName
    }
    if (-not $afterProject.StartsWith("/")) {
        $url += '/'
    }
    $url += $afterProject
    if ($afterProject.IndexOf('?') -ne -1) {
        $url += "&"
    } else {
        $url += "?"
    }
    $url += "api-version=$version"
    $response = Invoke-RestMethod -Method Get -Uri $url -ContentType 'application/json' -Headers $v.RequestHeaders -Verbose:$VerbosePreference
    $response
}