-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInvoke-AzDoRestMethod.ps1
More file actions
107 lines (94 loc) · 2.93 KB
/
Invoke-AzDoRestMethod.ps1
File metadata and controls
107 lines (94 loc) · 2.93 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function Invoke-AzDoRestMethod {
<#
.SYNOPSIS
A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
A longer description of the function, its purpose, common use cases, etc.
.NOTES
Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
Test-MyTestFunction -Verbose
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[string]
$Uri,
[Parameter(Mandatory)]
[string]
$Version,
[Parameter()]
[string]
$QueryParameters,
[Parameter(Mandatory)]
[ValidateSet('GET', 'POST', 'PATCH', 'DELETE', "PUT")]
[string]
$Method,
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[PSCustomObject[]]
$Body,
[Parameter()]
[ValidateSet('application/json', 'application/json-patch+json')]
[string]
$ContentType = 'application/json'
)
begin {
Write-Verbose "Starting function: Invoke-AzDoRestMethod"
Write-Debug "uri: $uri"
Write-Debug "version: $Version"
Write-Debug "method: $Method"
Write-Debug "body: $($body | ConvertTo-Json -Depth 10)"
if ($script:header.Authorization -match "Bearer") {
$params = @{
Uri = "https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=6.0"
Method = 'GET'
Headers = $script:header
ContentType = 'application/json'
}
try {
$profileData = Invoke-RestMethod @params
if (!$profileData.id) {
throw
}
} catch {
Write-Verbose "Refreshing authentication header"
$script:header = $null
}
}
if (-not($script:header)) {
try {
New-AzDoAuthHeader -ErrorAction Stop
} catch {
throw $_
}
}
$params = @{
Method = $Method
Headers = $script:header
ContentType = $ContentType
}
if ($QueryParameters) {
$params.Uri = "$($Uri)?$($QueryParameters)&api-version=$($Version)"
} else {
$params.Uri = "$($Uri)?api-version=$($Version)"
}
Write-Verbose "Uri: $($params.Uri)"
Write-Verbose "Method: $($params.Method)"
}
process {
if ($Method -in @('POST', 'PATCH', "PUT")) {
Write-Verbose "Body: $($Body | ConvertTo-Json -Depth 10)"
$params.Body = $Body | ConvertTo-Json -Depth 10
}
if ($PSCmdlet.ShouldProcess($ProjectName, "Invoke Rest method on: $($PSStyle.Bold)$ProjectName$($PSStyle.Reset)")) {
try {
Invoke-RestMethod @params
} catch {
$PSCmdlet.ThrowTerminatingError((Write-AzdoError -Message ($_ | ConvertFrom-Json).message))
}
}
}
}