Skip to content

Commit 84e8f2d

Browse files
committed
functionize text replacement
1 parent 5bd7b4b commit 84e8f2d

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function Get-CIPPTextReplacement {
2+
<#
3+
.SYNOPSIS
4+
Replaces text with tenant specific values
5+
.DESCRIPTION
6+
Helper function to replace text with tenant specific values
7+
.PARAMETER TenantFilter
8+
The tenant filter to use
9+
.PARAMETER Text
10+
The text to replace
11+
.EXAMPLE
12+
Get-CIPPTextReplacement -TenantFilter 'contoso.com' -Text 'Hello %tenantname%'
13+
#>
14+
param (
15+
[string]$TenantFilter,
16+
[string]$Text
17+
)
18+
$Tenant = Get-Tenants -TenantFilter $TenantFilter
19+
$CustomerId = $Tenant.customerId
20+
21+
#connect to table, get replacement map. This is for future usage. The replacement map will allow users to create custom vars that get replaced by the actual values per tenant. Example:
22+
# %WallPaperPath% gets replaced by RowKey WallPaperPath which is set to C:\Wallpapers for tenant 1, and D:\Wallpapers for tenant 2
23+
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'
24+
$ReplaceMap = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq '$CustomerId'"
25+
if ($ReplaceMap) {
26+
foreach ($Replace in $ReplaceMap) {
27+
$String = '%{0}%' -f $Replace.RowKey
28+
$Text = $Text -replace $String, $Replace.Value
29+
}
30+
}
31+
#default replacements for all tenants: %tenantid% becomes $tenant.customerId, %tenantfilter% becomes $tenant.defaultDomainName, %tenantname% becomes $tenant.displayName
32+
$Text = $Text -replace '%tenantid%', $Tenant.customerId
33+
$Text = $Text -replace '%tenantfilter%', $Tenant.defaultDomainName
34+
$Text = $Text -replace '%tenantname%', $Tenant.displayName
35+
36+
return $Text
37+
}

Modules/CIPPCore/Public/Set-CIPPIntunePolicy.ps1

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,8 @@ function Set-CIPPIntunePolicy {
1212
$tenantFilter
1313
)
1414
$APINAME = 'Set-CIPPIntunePolicy'
15-
#connect to table, get replacement map. This is for future usage. The replacement map will allow users to create custom vars that get replaced by the actual values per tenant. Example:
16-
# %WallPaperPath% gets replaced by RowKey WallPaperPath which is set to C:\Wallpapers for tenant 1, and D:\Wallpapers for tenant 2
17-
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'
18-
$ReplaceMap = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq '$tenantFilter'"
19-
if ($ReplaceMap) {
20-
foreach ($Replace in $ReplaceMap) {
21-
$String = '%{0}%' -f $Replace.RowKey
22-
$RawJSON = $RawJSON -replace $String, $Replace.Value
23-
}
24-
}
25-
#default replacements for all tenants: %tenantid% becomes $tenant.customerId, %tenantfilter% becomes $tenant.defaultDomainName, %tenantname% becomes $tenant.displayName
26-
$Tenant = Get-Tenants -TenantFilter $tenantFilter
27-
$RawJSON = $RawJSON -replace '%tenantid%', $Tenant.customerId
28-
$RawJSON = $RawJSON -replace '%tenantfilter%', $Tenant.defaultDomainName
29-
$RawJSON = $RawJSON -replace '%tenantname%', $Tenant.displayName
30-
15+
16+
$RawJSON = Get-CIPPTextReplacement -TenantFilter $tenantFilter -Text $RawJSON
3117

3218
try {
3319
switch ($TemplateType) {

0 commit comments

Comments
 (0)