Skip to content

Commit 48d447d

Browse files
Merge pull request KelvinTegelaar#1630 from PeterVive/standardbranding-createnonexistantdefault
Invoke-CIPPStandardBranding: Create default branding localization if doesn't exist
2 parents 6b668bb + 300f3d8 commit 48d447d

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardBranding.ps1

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,62 @@ function Invoke-CIPPStandardBranding {
3838

3939
$TenantId = Get-Tenants | Where-Object -Property defaultDomainName -EQ $Tenant
4040

41-
try {
42-
$CurrentState = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/organization/$($TenantId.customerId)/branding/localizations/0" -tenantID $Tenant -AsApp $true
43-
}
44-
catch {
45-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
46-
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the Branding state for $Tenant. Error: $ErrorMessage" -Sev Error
47-
return
48-
}
49-
41+
$Localizations = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/organization/$($TenantId.customerId)/branding/localizations" -tenantID $Tenant -AsApp $true
5042
# Get layoutTemplateType value using null-coalescing operator
5143
$layoutTemplateType = $Settings.layoutTemplateType.value ?? $Settings.layoutTemplateType
44+
# If default localization (id "0") exists, use that to get the currentState. Otherwise we have to create it first.
45+
if ($Localizations | Where-Object { $_.id -eq '0' }) {
46+
try {
47+
$CurrentState = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/organization/$($TenantId.customerId)/branding/localizations/0" -tenantID $Tenant -AsApp $true
48+
}
49+
catch {
50+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
51+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the Branding state for $Tenant. Error: $ErrorMessage" -Sev Error
52+
return
53+
}
54+
}
55+
else {
56+
try {
57+
$GraphRequest = @{
58+
tenantID = $Tenant
59+
uri = "https://graph.microsoft.com/beta/organization/$($TenantId.customerId)/branding/localizations"
60+
AsApp = $true
61+
Type = 'POST'
62+
ContentType = 'application/json; charset=utf-8'
63+
Body = [pscustomobject]@{
64+
signInPageText = $Settings.signInPageText
65+
usernameHintText = $Settings.usernameHintText
66+
loginPageTextVisibilitySettings = [pscustomobject]@{
67+
hideAccountResetCredentials = $Settings.hideAccountResetCredentials
68+
}
69+
loginPageLayoutConfiguration = [pscustomobject]@{
70+
layoutTemplateType = $layoutTemplateType
71+
isHeaderShown = $Settings.isHeaderShown
72+
isFooterShown = $Settings.isFooterShown
73+
}
74+
} | ConvertTo-Json -Compress
75+
}
76+
$CurrentState = New-GraphPostRequest @GraphRequest
77+
}
78+
catch {
79+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
80+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not create the default Branding localization for $Tenant. Error: $ErrorMessage" -Sev Error
81+
return
82+
}
83+
}
5284

5385
$StateIsCorrect = ($CurrentState.signInPageText -eq $Settings.signInPageText) -and
54-
($CurrentState.usernameHintText -eq $Settings.usernameHintText) -and
55-
($CurrentState.loginPageTextVisibilitySettings.hideAccountResetCredentials -eq $Settings.hideAccountResetCredentials) -and
56-
($CurrentState.loginPageLayoutConfiguration.layoutTemplateType -eq $layoutTemplateType) -and
57-
($CurrentState.loginPageLayoutConfiguration.isHeaderShown -eq $Settings.isHeaderShown) -and
58-
($CurrentState.loginPageLayoutConfiguration.isFooterShown -eq $Settings.isFooterShown)
86+
($CurrentState.usernameHintText -eq $Settings.usernameHintText) -and
87+
($CurrentState.loginPageTextVisibilitySettings.hideAccountResetCredentials -eq $Settings.hideAccountResetCredentials) -and
88+
($CurrentState.loginPageLayoutConfiguration.layoutTemplateType -eq $layoutTemplateType) -and
89+
($CurrentState.loginPageLayoutConfiguration.isHeaderShown -eq $Settings.isHeaderShown) -and
90+
($CurrentState.loginPageLayoutConfiguration.isFooterShown -eq $Settings.isFooterShown)
5991

6092
If ($Settings.remediate -eq $true) {
6193
if ($StateIsCorrect -eq $true) {
6294
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message 'Branding is already applied correctly.' -Sev Info
63-
} else {
95+
}
96+
else {
6497
try {
6598
$GraphRequest = @{
6699
tenantID = $Tenant
@@ -83,7 +116,8 @@ function Invoke-CIPPStandardBranding {
83116
}
84117
$null = New-GraphPostRequest @GraphRequest
85118
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message 'Successfully updated branding.' -Sev Info
86-
} catch {
119+
}
120+
catch {
87121
$ErrorMessage = Get-CippException -Exception $_
88122
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Failed to update branding. Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage
89123
}
@@ -95,7 +129,8 @@ function Invoke-CIPPStandardBranding {
95129

96130
if ($StateIsCorrect -eq $true) {
97131
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message 'Branding is correctly set.' -Sev Info
98-
} else {
132+
}
133+
else {
99134
Write-StandardsAlert -message 'Branding is incorrectly set.' -object ($CurrentState | Select-Object -Property signInPageText, usernameHintText, loginPageTextVisibilitySettings, loginPageLayoutConfiguration) -tenant $Tenant -standardName 'Branding' -standardId $Settings.standardId
100135
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message 'Branding is incorrectly set.' -Sev Info
101136
}

0 commit comments

Comments
 (0)