-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect-AutGraph.ps1
More file actions
85 lines (65 loc) · 3.19 KB
/
Connect-AutGraph.ps1
File metadata and controls
85 lines (65 loc) · 3.19 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
# https://gcits.com/knowledge-base/export-customers-microsoft-secure-scores-to-csv-and-html-reports/
# Function is based on this Securescore report
Function Connect-AutGraph {
[CmdletBinding()]
Param(
[ValidateSet("V1.0","Beta")]$GraphVersion = "Beta",
[ValidateSet("manage.microsoft.com/api","graph.microsoft.com","manage.office.com/api")]$APIProvider ="Graph.Microsoft.Com",
$EndPoint = "Users",
[validateset ("Yes","No")]$All="NO",
[ValidateSet ("0","3","5","7")]$Retry = "0",
[Parameter(Mandatory = $True)]$access_Token)
# will add a check here for other api endpoints like azure to create the endpoint.
$URIForApplicationPermissionCall = "https://$ApiProvider/$GraphVersion/$EndPoint"
# Try to access graph information X times
$Stoploop = $false
[int]$Retrycount = "1"
do {
try {
$GraphResponse = $null
$GraphResponse = (Invoke-RestMethod `
-Uri $URIForApplicationPermissionCall `
-Headers @{"Authorization" = "Bearer $access_token"} `
-Method GET -DisableKeepAlive -UseBasicParsing) # -ContentType "application/json"
$Stoploop = $true
}
catch {
if ($Retrycount -gt $Retry) {
$Stoploop = $true
throw $Error[0].Exception
}
else {
Write-Host "Could not get Graph content. Retrying in 5 seconds..." -ForegroundColor DarkYellow
Start-Sleep -Seconds 5
$Retrycount ++
throw $Error[0].Exception
}
}
}
While ($Stoploop -eq $false)
# Added this check because some endpoints gives you the return data in ().value and some endpoints just gives normal info,
# so this is to check what kind of information we receive back
if ($graphresponse.Psobject.Properties.Name -notcontains "value") {
$GraphOutput = $GraphResponse
}
else {
$GraphOutput = $GraphResponse.value
}
# Check if there is presented a next link if the result is paged, then loop trough until all information is collected if NextLink parameter is set to Yes
$nextGraphLink = $graphResponse.'@odata.nextLink'
if ($All -like "Yes") {
while ($null -ne $NextGraphLink){
Write-Verbose "Next link is: $nextGraphLink"
$graphResponse = (Invoke-RestMethod -Method Get -Uri $nextGraphLink -Headers @{"Authorization"="Bearer $access_Token"} -UseBasicParsing)
$nextGraphLink = $graphResponse.'@odata.nextLink'
if ($graphresponse.Psobject.Properties.Name -notcontains "value") {
$GraphOutput += $GraphResponse
}
Else {
$GraphOutput += $GraphResponse.Value
}
Start-Sleep -Seconds 1
}
}
Return $GraphOutput
}