|
1 | 1 | using namespace System.Net |
2 | 2 |
|
3 | 3 | Function Invoke-ExecGraphRequest { |
4 | | - <# |
| 4 | + <# |
5 | 5 | .FUNCTIONALITY |
6 | 6 | Entrypoint |
7 | 7 | #> |
8 | | - [CmdletBinding()] |
9 | | - param($Request, $TriggerMetadata) |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
10 | 10 |
|
11 | | - $APIName = $Request.Params.CIPPEndpoint |
12 | | - Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Accessed this API' -Sev 'Debug' |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $Headers = $Request.Headers |
| 13 | + Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
13 | 14 |
|
14 | | - Function ConvertTo-FlatObject { |
15 | | - # https://evotec.xyz/powershell-converting-advanced-object-to-flat-object/ - MIT License |
16 | | - [CmdletBinding()] |
17 | | - Param ( |
18 | | - [Parameter(ValueFromPipeLine)][Object[]]$Objects, |
19 | | - [String]$Separator = '.', |
20 | | - [ValidateSet('', 0, 1)]$Base = 1, |
21 | | - [int]$Depth = 5, |
22 | | - [Parameter(DontShow)][String[]]$Path, |
23 | | - [Parameter(DontShow)][System.Collections.IDictionary] $OutputObject |
24 | | - ) |
25 | | - Begin { |
26 | | - $InputObjects = [System.Collections.Generic.List[Object]]::new() |
27 | | - } |
28 | | - Process { |
29 | | - foreach ($O in $Objects) { |
30 | | - $InputObjects.Add($O) |
| 15 | + Function ConvertTo-FlatObject { |
| 16 | + # https://evotec.xyz/powershell-converting-advanced-object-to-flat-object/ - MIT License |
| 17 | + [CmdletBinding()] |
| 18 | + Param ( |
| 19 | + [Parameter(ValueFromPipeLine)][Object[]]$Objects, |
| 20 | + [String]$Separator = '.', |
| 21 | + [ValidateSet('', 0, 1)]$Base = 1, |
| 22 | + [int]$Depth = 5, |
| 23 | + [Parameter(DontShow)][String[]]$Path, |
| 24 | + [Parameter(DontShow)][System.Collections.IDictionary] $OutputObject |
| 25 | + ) |
| 26 | + Begin { |
| 27 | + $InputObjects = [System.Collections.Generic.List[Object]]::new() |
| 28 | + } |
| 29 | + Process { |
| 30 | + foreach ($O in $Objects) { |
| 31 | + $InputObjects.Add($O) |
| 32 | + } |
| 33 | + } |
| 34 | + End { |
| 35 | + If ($PSBoundParameters.ContainsKey('OutputObject')) { |
| 36 | + $Object = $InputObjects[0] |
| 37 | + $Iterate = [ordered] @{} |
| 38 | + if ($null -eq $Object) { |
| 39 | + #Write-Verbose -Message "ConvertTo-FlatObject - Object is null" |
| 40 | + } elseif ($Object.GetType().Name -in 'String', 'DateTime', 'TimeSpan', 'Version', 'Enum') { |
| 41 | + $Object = $Object.ToString() |
| 42 | + } elseif ($Depth) { |
| 43 | + $Depth-- |
| 44 | + If ($Object -is [System.Collections.IDictionary]) { |
| 45 | + $Iterate = $Object |
| 46 | + } elseif ($Object -is [Array] -or $Object -is [System.Collections.IEnumerable]) { |
| 47 | + $i = $Base |
| 48 | + foreach ($Item in $Object.GetEnumerator()) { |
| 49 | + $Iterate["$i"] = $Item |
| 50 | + $i += 1 |
31 | 51 | } |
32 | | - } |
33 | | - End { |
34 | | - If ($PSBoundParameters.ContainsKey('OutputObject')) { |
35 | | - $Object = $InputObjects[0] |
36 | | - $Iterate = [ordered] @{} |
37 | | - if ($null -eq $Object) { |
38 | | - #Write-Verbose -Message "ConvertTo-FlatObject - Object is null" |
39 | | - } elseif ($Object.GetType().Name -in 'String', 'DateTime', 'TimeSpan', 'Version', 'Enum') { |
40 | | - $Object = $Object.ToString() |
41 | | - } elseif ($Depth) { |
42 | | - $Depth-- |
43 | | - If ($Object -is [System.Collections.IDictionary]) { |
44 | | - $Iterate = $Object |
45 | | - } elseif ($Object -is [Array] -or $Object -is [System.Collections.IEnumerable]) { |
46 | | - $i = $Base |
47 | | - foreach ($Item in $Object.GetEnumerator()) { |
48 | | - $Iterate["$i"] = $Item |
49 | | - $i += 1 |
50 | | - } |
51 | | - } else { |
52 | | - foreach ($Prop in $Object.PSObject.Properties) { |
53 | | - if ($Prop.IsGettable) { |
54 | | - $Iterate["$($Prop.Name)"] = $Object.$($Prop.Name) |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - If ($Iterate.Keys.Count) { |
60 | | - foreach ($Key in $Iterate.Keys) { |
61 | | - ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject |
62 | | - } |
63 | | - } else { |
64 | | - $Property = $Path -Join $Separator |
65 | | - $OutputObject[$Property] = $Object |
66 | | - } |
67 | | - } elseif ($InputObjects.Count -gt 0) { |
68 | | - foreach ($ItemObject in $InputObjects) { |
69 | | - $OutputObject = [ordered]@{} |
70 | | - ConvertTo-FlatObject -Objects @(, $ItemObject) -Separator $Separator -Base $Base -Depth $Depth -Path $Path -OutputObject $OutputObject |
71 | | - [PSCustomObject] $OutputObject |
72 | | - } |
| 52 | + } else { |
| 53 | + foreach ($Prop in $Object.PSObject.Properties) { |
| 54 | + if ($Prop.IsGettable) { |
| 55 | + $Iterate["$($Prop.Name)"] = $Object.$($Prop.Name) |
| 56 | + } |
73 | 57 | } |
| 58 | + } |
74 | 59 | } |
75 | | - } |
76 | | - $TenantFilter = $Request.Query.TenantFilter |
77 | | - try { |
78 | | - if ($TenantFilter -ne 'AllTenants') { |
79 | | - $RawGraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($Request.Query.Endpoint)" -tenantid $TenantFilter -NoPagination [boolean]$Request.query.DisablePagination -ComplexFilter |
| 60 | + If ($Iterate.Keys.Count) { |
| 61 | + foreach ($Key in $Iterate.Keys) { |
| 62 | + ConvertTo-FlatObject -Objects @(, $Iterate["$Key"]) -Separator $Separator -Base $Base -Depth $Depth -Path ($Path + $Key) -OutputObject $OutputObject |
| 63 | + } |
80 | 64 | } else { |
81 | | - $RawGraphRequest = Get-Tenants | ForEach-Object -Parallel { |
82 | | - Import-Module '.\Modules\AzBobbyTables' |
83 | | - Import-Module '.\Modules\CIPPCore' |
84 | | - try { |
85 | | - $DefaultDomainName = $_.defaultDomainName |
86 | | - $TenantName = $_.displayName |
87 | | - New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($using:Request.Query.Endpoint)" -tenantid $DefaultDomainName -NoPagination [boolean]$using:Request.query.DisablePagination -ComplexFilter | Select-Object @{ |
88 | | - label = 'Tenant' |
89 | | - expression = { $TenantName } |
90 | | - }, * |
91 | | - } catch { |
92 | | - continue |
93 | | - } |
94 | | - } |
95 | | - |
| 65 | + $Property = $Path -Join $Separator |
| 66 | + $OutputObject[$Property] = $Object |
| 67 | + } |
| 68 | + } elseif ($InputObjects.Count -gt 0) { |
| 69 | + foreach ($ItemObject in $InputObjects) { |
| 70 | + $OutputObject = [ordered]@{} |
| 71 | + ConvertTo-FlatObject -Objects @(, $ItemObject) -Separator $Separator -Base $Base -Depth $Depth -Path $Path -OutputObject $OutputObject |
| 72 | + [PSCustomObject] $OutputObject |
96 | 73 | } |
97 | | - $GraphRequest = $RawGraphRequest | Where-Object -Property '@odata.context' -EQ $null | ConvertTo-FlatObject |
98 | | - $StatusCode = [HttpStatusCode]::OK |
99 | | - } catch { |
100 | | - $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
101 | | - $StatusCode = [HttpStatusCode]::Forbidden |
102 | | - $GraphRequest = $ErrorMessage |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + $TenantFilter = $Request.Query.TenantFilter |
| 78 | + try { |
| 79 | + if ($TenantFilter -ne 'AllTenants') { |
| 80 | + $RawGraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($Request.Query.Endpoint)" -tenantid $TenantFilter -NoPagination [boolean]$Request.query.DisablePagination -ComplexFilter |
| 81 | + } else { |
| 82 | + $RawGraphRequest = Get-Tenants | ForEach-Object -Parallel { |
| 83 | + Import-Module '.\Modules\AzBobbyTables' |
| 84 | + Import-Module '.\Modules\CIPPCore' |
| 85 | + try { |
| 86 | + $DefaultDomainName = $_.defaultDomainName |
| 87 | + $TenantName = $_.displayName |
| 88 | + New-GraphGetRequest -uri "https://graph.microsoft.com/beta/$($using:Request.Query.Endpoint)" -tenantid $DefaultDomainName -NoPagination [boolean]$using:Request.query.DisablePagination -ComplexFilter | Select-Object @{ |
| 89 | + label = 'Tenant' |
| 90 | + expression = { $TenantName } |
| 91 | + }, * |
| 92 | + } catch { |
| 93 | + continue |
| 94 | + } |
| 95 | + } |
| 96 | + |
103 | 97 | } |
| 98 | + $GraphRequest = $RawGraphRequest | Where-Object -Property '@odata.context' -EQ $null | ConvertTo-FlatObject |
| 99 | + $StatusCode = [HttpStatusCode]::OK |
| 100 | + } catch { |
| 101 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 102 | + $StatusCode = [HttpStatusCode]::Forbidden |
| 103 | + $GraphRequest = $ErrorMessage |
| 104 | + } |
104 | 105 |
|
105 | | - # Associate values to output bindings by calling 'Push-OutputBinding'. |
106 | | - Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
107 | | - StatusCode = $StatusCode |
108 | | - Body = @($GraphRequest) |
109 | | - }) |
| 106 | + # Associate values to output bindings by calling 'Push-OutputBinding'. |
| 107 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 108 | + StatusCode = $StatusCode |
| 109 | + Body = @($GraphRequest) |
| 110 | + }) |
110 | 111 |
|
111 | 112 | } |
0 commit comments