-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAPI-ListSensitivityLabels.ps1
137 lines (98 loc) · 3.68 KB
/
API-ListSensitivityLabels.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#Requires -Modules @{ ModuleName="MicrosoftPowerBIMgmt"; ModuleVersion="1.2.1077" }
param (
$graphUrl = "https://graph.microsoft.com/beta",
$apiResource = "https://graph.microsoft.com",
$servicePrincipalId = "",
$servicePrincipalSecret = "",
$servicePrincipalTenantId = ""
)
$ErrorActionPreference = "Stop"
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
Set-Location $currentPath
function Get-AuthToken {
[cmdletbinding()]
param
(
[string]
$authority = "https://#.microsoftonline.com",
[string]
$tenantid,
[string]
$appid,
[string]
$appsecret ,
[string]
$resource
)
write-verbose "getting authentication token"
$granttype = "client_credentials"
$tokenuri = "$authority/$tenantid/oauth2/token?api-version=1.0"
$appsecret = [System.Web.HttpUtility]::urlencode($appsecret)
$body = "grant_type=$granttype&client_id=$appid&resource=$resource&client_secret=$appsecret"
$token = invoke-restmethod -method post -uri $tokenuri -body $body
$accesstoken = $token.access_token
write-output $accesstoken
}
function Read-FromGraphAPI {
[CmdletBinding()]
param
(
[string]
$url,
[string]
$accessToken,
[string]
$format = "JSON"
)
#https://blogs.msdn.microsoft.com/exchangedev/2017/04/07/throttling-coming-to-outlook-api-and-microsoft-graph/
try {
$headers = @{
'Content-Type' = "application/json"
'Authorization' = "Bearer $accessToken"
}
$result = Invoke-RestMethod -Method Get -Uri $url -Headers $headers
if ($format -eq "CSV") {
ConvertFrom-CSV -InputObject $result | Write-Output
}
else {
Write-Output $result.value
while ($result.'@odata.nextLink') {
$result = Invoke-RestMethod -Method Get -Uri $result.'@odata.nextLink' -Headers $headers
Write-Output $result.value
}
}
}
catch [System.Net.WebException] {
$ex = $_.Exception
try {
$statusCode = $ex.Response.StatusCode
if ($statusCode -eq 429) {
$message = "429 Throthling Error - Sleeping..."
Write-Host $message
Start-Sleep -Seconds 1000
}
else {
if ($ex.Response -ne $null) {
$statusCode = $ex.Response.StatusCode
$stream = $ex.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$errorContent = $reader.ReadToEnd()
$message = "$($ex.Message) - '$errorContent'"
}
else {
$message = "$($ex.Message) - 'Empty'"
}
}
Write-Error -Exception $ex -Message $message
}
finally {
if ($reader) { $reader.Dispose() }
if ($stream) { $stream.Dispose() }
}
}
}
$authToken = Get-AuthToken -resource $apiResource -appid $servicePrincipalId -appsecret $servicePrincipalSecret -tenantid $servicePrincipalTenantId
$data = Read-FromGraphAPI -accessToken $authToken -url "$graphUrl/security/informationProtection/sensitivityLabels" | select * -ExcludeProperty "@odata.id"
$data | Format-Table