Skip to content

Commit bd2afc2

Browse files
committed
feat: improve text replacements in Graph Request
1 parent a7aee30 commit bd2afc2

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ListGraphRequest.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function Invoke-ListGraphRequest {
1818

1919
$Parameters = @{}
2020
if ($Request.Query.'$filter') {
21-
$Parameters.'$filter' = $Request.Query.'$filter' -replace '%tenantid%', $env:TenantID
21+
$Parameters.'$filter' = $Request.Query.'$filter'
2222
}
2323

2424
if (!$Request.Query.'$filter' -and $Request.Query.graphFilter) {
25-
$Parameters.'$filter' = $Request.Query.graphFilter -replace '%tenantid%', $env:TenantID
25+
$Parameters.'$filter' = $Request.Query.graphFilter
2626
}
2727

2828
if ($Request.Query.'$select') {

Modules/CIPPCore/Public/Get-CIPPTextReplacement.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ function Get-CIPPTextReplacement {
1313
#>
1414
param (
1515
[string]$TenantFilter,
16-
[string]$Text
16+
$Text
1717
)
18+
if ($Text -isnot [string]) {
19+
return $Text
20+
}
21+
1822
$Tenant = Get-Tenants -TenantFilter $TenantFilter
1923
$CustomerId = $Tenant.customerId
2024

@@ -47,5 +51,8 @@ function Get-CIPPTextReplacement {
4751
$Text = $Text -replace '%tenantfilter%', $Tenant.defaultDomainName
4852
$Text = $Text -replace '%tenantname%', $Tenant.displayName
4953

54+
# Partner specific replacements
55+
$Text = $Text -replace '%partnertenantid%', $ENV:TenantID
56+
$Text = $Text -replace '%samappid%', $ENV:ApplicationID
5057
return $Text
5158
}

Modules/CIPPCore/Public/GraphRequests/Get-GraphRequestList.ps1

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ function Get-GraphRequestList {
9595
$GraphQuery.Query = $ParamCollection.ToString()
9696
$PartitionKey = Get-StringHash -String (@($Endpoint, $ParamCollection.ToString()) -join '-')
9797
Write-Information "PK: $PartitionKey"
98-
Write-Information ( 'GET [ {0} ]' -f $GraphQuery.ToString())
9998

10099
# Perform $count check before caching
101100
$Count = 0
@@ -117,12 +116,27 @@ function Get-GraphRequestList {
117116
if ($AsApp) {
118117
$GraphRequest.asApp = $AsApp
119118
}
119+
120+
if ($Endpoint -match '%' -or $Parameters.Values -match '%') {
121+
$TenantId = (Get-Tenants -IncludeErrors | Where-Object { $_.defaultDomainName -eq $TenantFilter -or $_.customerId -eq $TenantFilter }).customerId
122+
$Endpoint = Get-CIPPTextReplacement -TenantFilter $TenantFilter -Text $Endpoint
123+
$GraphQuery = [System.UriBuilder]('https://graph.microsoft.com/{0}/{1}' -f $Version, $Endpoint)
124+
$ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
125+
foreach ($Item in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) {
126+
$Value = Get-CIPPTextReplacement -TenantFilter $TenantFilter -Text $Item.Value
127+
$ParamCollection.Add($Item.Key, $Value)
128+
}
129+
$GraphQuery.Query = $ParamCollection.ToString()
130+
$GraphRequest.uri = $GraphQuery.ToString()
131+
}
132+
120133
if ($Parameters.'$count' -and !$SkipCache.IsPresent -and !$NoPagination.IsPresent) {
121134
$Count = New-GraphGetRequest @GraphRequest -CountOnly -ErrorAction Stop
122135
if ($CountOnly.IsPresent) { return $Count }
123136
Write-Information "Total results (`$count): $Count"
124137
}
125138
}
139+
Write-Information ( 'GET [ {0} ]' -f $GraphQuery.ToString())
126140

127141
try {
128142
if ($QueueId) {
@@ -153,31 +167,6 @@ function Get-GraphRequestList {
153167
Write-Information $_.InvocationInfo.PositionMessage
154168
}
155169

156-
if ($TenantFilter -ne 'AllTenants' -and $Endpoint -match '%tenantid%') {
157-
Write-Information "Replacing TenantId in endpoint with $TenantFilter"
158-
$TenantId = (Get-Tenants -IncludeErrors | Where-Object { $_.defaultDomainName -eq $TenantFilter -or $_.customerId -eq $TenantFilter }).customerId
159-
$Endpoint = $Endpoint -replace '%tenantid%', $TenantId
160-
$GraphQuery = [System.UriBuilder]('https://graph.microsoft.com/{0}/{1}' -f $Version, $Endpoint)
161-
$ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
162-
foreach ($Item in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) {
163-
$ParamCollection.Add($Item.Key, $Item.Value -replace '%tenantid%', $TenantId)
164-
}
165-
$GraphQuery.Query = $ParamCollection.ToString()
166-
$GraphRequest.uri = $GraphQuery.ToString()
167-
}
168-
169-
if ($TenantFilter -ne 'AllTenants' -and $Endpoint -match '%appid%') {
170-
Write-Information "Replacing AppId in endpoint with $env:ApplicationID"
171-
$Endpoint = $Endpoint -replace '%appid%', $env:ApplicationID
172-
$GraphQuery = [System.UriBuilder]('https://graph.microsoft.com/{0}/{1}' -f $Version, $Endpoint)
173-
$ParamCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
174-
foreach ($Item in ($Parameters.GetEnumerator() | Sort-Object -CaseSensitive -Property Key)) {
175-
$ParamCollection.Add($Item.Key, $Item.Value -replace '%appid%', $env:ApplicationID)
176-
}
177-
$GraphQuery.Query = $ParamCollection.ToString()
178-
$GraphRequest.uri = $GraphQuery.ToString()
179-
}
180-
181170
if (!$Rows) {
182171
switch ($TenantFilter) {
183172
'AllTenants' {

0 commit comments

Comments
 (0)