-
Notifications
You must be signed in to change notification settings - Fork 17
/
azure-az-rest-query.ps1
93 lines (81 loc) · 2.4 KB
/
azure-az-rest-query.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<#
.SYNOPSIS
https://docs.microsoft.com/en-us/rest/api/
https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines
.\azure-az-rest-query.ps1 -query resources
.\azure-az-rest-query.ps1 -query resourceGroups
.\azure-az-rest-query.ps1 -query providers/Microsoft.Compute
.LINK
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-rest-query.ps1" -outFile "$pwd\azure-az-rest-query.ps1";
.\azure-az-rest-query.ps1
#>
param(
$token = $global:token,
$SubscriptionID = (Get-azContext).Subscription.Id,
$apiVersion = "2019-03-01",
$baseURI = "https://management.azure.com/subscriptions/$($SubscriptionID)/",
$query,
$arguments,
[ValidateSet("get","post","put")]
$method = "get",
[ValidateSet('application/x-www-form-urlencoded','application/json','application/xml')]
$contentType = 'application/x-www-form-urlencoded',
$clientid = ($global:clientId, '1950a258-227b-4e31-a9cf-717495945fc2' -ne $null)[0],
$body=@{},
$headers=@{}
)
$epochTime = [int64](([datetime]::UtcNow)-(get-date "1/1/1970")).TotalSeconds
if(!$token)
{
write-warning "supply token for token argument or run azure-az-rest-logon.ps1 to generate bearer token"
return
}
elseif($token.expires_on -le $epochTime)
{
$token
write-warning "expired token. run azure-az-rest-logon.ps1 to generate bearer token"
$global:token = $null
return
}
if($apiVersion)
{
$uri = $baseURI + $query + "?api-version=" + $apiVersion + $arguments
}
else
{
$uri = $baseURI + $query + $arguments
}
$uri
if($contentType -imatch "json")
{
$body = convertto-json $body
}
$body
if($headers.Count -lt 1)
{
$headers = @{
'authorization' = "Bearer $($Token.access_token)"
'accept' = "*/*" #'application/json'
'client_id' = $clientid
}
}
$params = @{
ContentType = $contentType
Headers = $headers
Method = $method
uri = $uri
Body = $body
}
$params
$error.Clear()
$response = Invoke-RestMethod @params -Verbose -Debug
#$response = Invoke-WebRequest @params -Verbose -Debug
$global:response = $response
write-host (ConvertTo-Json -Depth 99 ($global:response))
Write-Output $global:response
write-host "output saved in `$global:response" -ForegroundColor Yellow
if($error)
{
return 1
}