forked from KelvinTegelaar/CIPP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.ps1
112 lines (106 loc) · 5.33 KB
/
run.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
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
$APIName = $TriggerMetadata.FunctionName
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
Function ConvertTo-FlatObject {
# https://evotec.xyz/powershell-converting-advanced-object-to-flat-object/ - MIT License
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeLine)][Object[]]$Objects,
[String]$Separator = '.',
[ValidateSet('', 0, 1)]$Base = 1,
[int]$Depth = 5,
[Parameter(DontShow)][String[]]$Path,
[Parameter(DontShow)][System.Collections.IDictionary] $OutputObject
)
Begin {
$InputObjects = [System.Collections.Generic.List[Object]]::new()
}
Process {
foreach ($O in $Objects) {
$InputObjects.Add($O)
}
}
End {
If ($PSBoundParameters.ContainsKey('OutputObject')) {
$Object = $InputObjects[0]
$Iterate = [ordered] @{}
if ($null -eq $Object) {
#Write-Verbose -Message "ConvertTo-FlatObject - Object is null"
}
elseif ($Object.GetType().Name -in 'String', 'DateTime', 'TimeSpan', 'Version', 'Enum') {
$Object = $Object.ToString()
}
elseif ($Depth) {
$Depth--
If ($Object -is [System.Collections.IDictionary]) {
$Iterate = $Object
}
elseif ($Object -is [Array] -or $Object -is [System.Collections.IEnumerable]) {
$i = $Base
foreach ($Item in $Object.GetEnumerator()) {
$Iterate["$i"] = $Item
$i += 1
}
}
else {
foreach ($Prop in $Object.PSObject.Properties) {
if ($Prop.IsGettable) {
$Iterate["$($Prop.Name)"] = $Object.$($Prop.Name)
}
}
}
}
If ($Iterate.Keys.Count) {
foreach ($Key in $Iterate.Keys) {
ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject
}
}
else {
$Property = $Path -Join $Separator
$OutputObject[$Property] = $Object
}
}
elseif ($InputObjects.Count -gt 0) {
foreach ($ItemObject in $InputObjects) {
$OutputObject = [ordered]@{}
ConvertTo-FlatObject -Objects @(, $ItemObject) -Separator $Separator -Base $Base -Depth $Depth -Path $Path -OutputObject $OutputObject
[PSCustomObject] $OutputObject
}
}
}
}
$TenantFilter = $Request.Query.TenantFilter
try {
if ($TenantFilter -ne 'AllTenants') {
$RawGraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($Request.Query.Endpoint)" -tenantid $TenantFilter -NoPagination [boolean]$Request.query.DisablePagination -ComplexFilter
}
else {
$RawGraphRequest = Get-tenants | ForEach-Object {
try {
$DefaultDomainName = $_.defaultDomainName
$TenantName = $_.displayName
New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($Request.Query.Endpoint)" -tenantid $DefaultDomainName -NoPagination [boolean]$Request.query.DisablePagination -ComplexFilter
}
catch {
continue
}
} | Select-Object @{
label = 'Tenant'
expression = { $TenantName }
}, *
}
$GraphRequest = $RawGraphRequest | Where-Object -Property '@odata.context' -EQ $null | ConvertTo-FlatObject
$StatusCode = [HttpStatusCode]::OK
}
catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$StatusCode = [HttpStatusCode]::Forbidden
$GraphRequest = $ErrorMessage
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $StatusCode
Body = @($GraphRequest)
})