forked from kine/NVRAppDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-WI.ps1
71 lines (66 loc) · 2.4 KB
/
Get-WI.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<#
.SYNOPSIS
Get Work Item
.DESCRIPTION
Read the work item from Azure DevOps
.Parameter accountName
Name of the Azure DevOps organization to use
.Parameter PAT
PAT to use when connecting to Azure DevOps
.Parameter OAuthToken
OAuthToken to use for athentication (e.g. System.AccessToken from build agent)
.Parameter WINo
Number of the Work Item
.Parameter WIUrl
URL of the Work Item
.Parameter Expand
Value for API parameter $expand (None, Relations, Fields, Links, All)
.OUTPUTS
return work item data
#>
function Get-WI
{
param(
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True,ParameterSetName='WINo')]
[string]$accountName,
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True,ParameterSetName='WINo')]
[string]$projectName,
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True,ParameterSetName='WINo')]
[string]$WINo,
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True,ParameterSetName='WIUrl')]
[string]$WIUrl,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[string]$PAT,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[string]$OAuthToken='',
[Parameter(ValueFromPipelineByPropertyName=$True)]
[ValidateSet('None', 'Relations', 'Fields', 'Links', 'All')]
$Expand='None'
)
$ErrorActionPreference = "Stop"
$Token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
if (-not $OAuthToken) {
Write-Verbose -Message 'PAT authorization used'
$Header = @{"Authorization" = "Basic "+$Token; "content-type" = "application/json"}
} else {
Write-Verbose -Message 'OAuth authorization used'
$Header = @{"Authorization" = "Bearer "+$OAuthToken; "content-type" = "application/json"}
}
if ($WIUrl) {
if (-not $WIUrl.Contains("api-version")) {
if ($WIUrl.Contains("?")) {
$requestUri = "$($WIUrl)&api-version=5.1"
} else {
$requestUri = "$($WIUrl)?api-version=5.1"
}
} else {
$requestUri = $WIUrl
}
} else {
$requestUri = "https://dev.azure.com/$accountName/$projectName/_apis/wit/workitems/$($WINo)?api-version=5.1"
}
$requestUri += "&`$expand=$($Expand)"
Write-Verbose -Message $requestUri
$response = Invoke-RestMethod -Uri $requestUri -Method Get -Headers $Header -Verbose
Return $response
}