-
Notifications
You must be signed in to change notification settings - Fork 214
/
ComplianceAPI_utils.ps1
51 lines (46 loc) · 1.83 KB
/
ComplianceAPI_utils.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
# This file contains utility functions for Compliance API
# Invokes request for the given compliance API call
# Aug 31st 2021
function Invoke-ComplianceAPIRequest
{
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[psobject]$Cookies,
[Parameter(Mandatory=$True)]
[String]$api,
[Parameter(Mandatory=$False)]
[String]$Method = "Get",
[Parameter(Mandatory=$False)]
[String]$Body = $null,
[Parameter(Mandatory=$False)]
[String]$ContentType = "application/json; charset=utf-8",
[Parameter(Mandatory=$False)]
[Hashtable]$Headers=@{}
)
Process
{
# Check the cookies
if(!($Cookies.'XSRF-TOKEN' -and $Cookies.sccauth))
{
Throw "XSRF-TOKEN and sccauth cookies required!"
}
$url = "https://compliance.microsoft.com/api/$api"
# First, add XSRF-TOKEN to headers
$Headers["X-XSRF-TOKEN"] = [System.Net.WebUtility]::UrlDecode($Cookies.'XSRF-TOKEN')
# Create a web session for the cookies
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add([System.Net.Cookie]::new("XSRF-TOKEN",$Cookies.'XSRF-TOKEN',"/","compliance.microsoft.com"))
$session.Cookies.Add([System.Net.Cookie]::new("sccauth", $Cookies.sccauth ,"/","compliance.microsoft.com"))
$session.Cookies.MaxCookieSize=65536
# Invoke the command
if($Method -eq "Put" -or $Method -eq "Post")
{
Invoke-RestMethod -UseBasicParsing -Method $Method -Uri $url -Headers $Headers -WebSession $session -Body $body -ContentType $ContentType
}
else
{
Invoke-RestMethod -UseBasicParsing -Method $Method -Uri $url -Headers $Headers -WebSession $session -ContentType $ContentType
}
}
}