Skip to content

Commit 7c7cabd

Browse files
Merge pull request KelvinTegelaar#1801 from KelvinTegelaar/dev
Only run DB update if change was made
2 parents a6b55ed + 3216056 commit 7c7cabd

6 files changed

+59
-32
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function Invoke-CIPPStandardAppDeploy {
111111
$CurrentValue = if ($MissingApps.Count -eq 0) { [PSCustomObject]@{'state' = 'Configured correctly' } } else { [PSCustomObject]@{'MissingApps' = $MissingApps } }
112112

113113
if ($Settings.remediate -eq $true) {
114+
$UpdateDB = $false
114115
if ($Mode -eq 'copy') {
115116
foreach ($App in $AppsToAdd) {
116117
$App = $App.Trim()
@@ -121,6 +122,7 @@ function Invoke-CIPPStandardAppDeploy {
121122
try {
122123
New-CIPPApplicationCopy -App $App -Tenant $Tenant
123124
Write-LogMessage -API 'Standards' -tenant $tenant -message "Added application $($Application.displayName) ($App) to $Tenant and updated it's permissions" -sev Info
125+
$UpdateDB = $true
124126
} catch {
125127
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
126128
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to add app $($Application.displayName) ($App). Error: $ErrorMessage" -sev Error
@@ -175,6 +177,7 @@ function Invoke-CIPPStandardAppDeploy {
175177
if ($InstantiateResult.application.appId) {
176178
Write-LogMessage -API 'Standards' -tenant $tenant -message "Successfully deployed Gallery Template $($TemplateData.AppName) to tenant $Tenant. Application ID: $($InstantiateResult.application.appId)" -sev Info
177179
New-CIPPApplicationCopy -App $InstantiateResult.application.appId -Tenant $Tenant
180+
$UpdateDB = $true
178181
} else {
179182
Write-LogMessage -API 'Standards' -tenant $tenant -message "Gallery Template deployment completed but application ID not returned for $($TemplateData.AppName) in tenant $Tenant" -sev Warning
180183
}
@@ -243,6 +246,7 @@ function Invoke-CIPPStandardAppDeploy {
243246
Add-CIPPDelegatedPermission -RequiredResourceAccess $CreatedApp.requiredResourceAccess -ApplicationId $CreatedApp.appId -Tenantfilter $Tenant
244247
Add-CIPPApplicationPermission -RequiredResourceAccess $CreatedApp.requiredResourceAccess -ApplicationId $CreatedApp.appId -Tenantfilter $Tenant
245248
}
249+
$UpdateDB = $true
246250
} else {
247251
Write-LogMessage -API 'Standards' -tenant $tenant -message "Application Manifest deployment failed - no application ID returned for $($TemplateData.AppName) in tenant $Tenant" -sev Error
248252
}
@@ -263,6 +267,7 @@ function Invoke-CIPPStandardAppDeploy {
263267
Add-CIPPApplicationPermission -TemplateId $TemplateId -TenantFilter $Tenant
264268
Add-CIPPDelegatedPermission -TemplateId $TemplateId -TenantFilter $Tenant
265269
Write-LogMessage -API 'Standards' -tenant $tenant -message "Added application $($TemplateData.AppName) from Enterprise App template and updated its permissions" -sev Info
270+
$UpdateDB = $true
266271
}
267272

268273
} catch {
@@ -272,11 +277,13 @@ function Invoke-CIPPStandardAppDeploy {
272277
}
273278
}
274279

275-
# Refresh service principals cache after remediation
276-
try {
277-
Set-CIPPDBCacheServicePrincipals -TenantFilter $Tenant
278-
} catch {
279-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh service principals cache after remediation: $($_.Exception.Message)" -sev Warning
280+
# Refresh service principals cache after remediation only if changes were made
281+
if ($UpdateDB) {
282+
try {
283+
Set-CIPPDBCacheServicePrincipals -TenantFilter $Tenant
284+
} catch {
285+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh service principals cache after remediation: $($_.Exception.Message)" -sev Warning
286+
}
280287
}
281288
}
282289

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function Invoke-CIPPStandardDisableResourceMailbox {
5757
}
5858

5959
if ($Settings.remediate -eq $true) {
60+
$UpdateDB = $false
6061
if ($ResourceMailboxList.Count -gt 0) {
6162
$int = 0
6263
$BulkRequests = foreach ($Mailbox in $ResourceMailboxList) {
@@ -80,6 +81,7 @@ function Invoke-CIPPStandardDisableResourceMailbox {
8081

8182
if ($result.status -eq 200 -or $result.status -eq 204) {
8283
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Entra account for $($Mailbox.RecipientTypeDetails), $($Mailbox.DisplayName), $($Mailbox.UserPrincipalName) disabled." -sev Info
84+
$UpdateDB = $true
8385
} else {
8486
$errorMsg = if ($result.body.error.message) { $result.body.error.message } else { "Unknown error (Status: $($result.status))" }
8587
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to disable Entra account for $($Mailbox.RecipientTypeDetails), $($Mailbox.DisplayName), $($Mailbox.UserPrincipalName): $errorMsg" -sev Error
@@ -90,11 +92,13 @@ function Invoke-CIPPStandardDisableResourceMailbox {
9092
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to process bulk disable resource mailboxes request: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
9193
}
9294

93-
# Refresh user cache after remediation
94-
try {
95-
Set-CIPPDBCacheUsers -TenantFilter $Tenant
96-
} catch {
97-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
95+
# Refresh user cache after remediation only if changes were made
96+
if ($UpdateDB) {
97+
try {
98+
Set-CIPPDBCacheUsers -TenantFilter $Tenant
99+
} catch {
100+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
101+
}
98102
}
99103
} else {
100104
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'All Entra accounts for resource mailboxes are already disabled.' -sev Info

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function Invoke-CIPPStandardDisableSharedMailbox {
5050
}
5151

5252
if ($Settings.remediate -eq $true) {
53+
$UpdateDB = $false
5354
if ($SharedMailboxList.Count -gt 0) {
5455
$int = 0
5556
$BulkRequests = foreach ($Mailbox in $SharedMailboxList) {
@@ -73,6 +74,7 @@ function Invoke-CIPPStandardDisableSharedMailbox {
7374

7475
if ($result.status -eq 200 -or $result.status -eq 204) {
7576
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Entra account for shared mailbox $($Mailbox.DisplayName) ($($Mailbox.ObjectKey)) disabled." -sev Info
77+
$UpdateDB = $true
7678
} else {
7779
$errorMsg = if ($result.body.error.message) { $result.body.error.message } else { "Unknown error (Status: $($result.status))" }
7880
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to disable Entra account for shared mailbox $($Mailbox.DisplayName) ($($Mailbox.ObjectKey)): $errorMsg" -sev Error
@@ -82,12 +84,14 @@ function Invoke-CIPPStandardDisableSharedMailbox {
8284
$ErrorMessage = Get-CippException -Exception $_
8385
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to process bulk disable shared mailboxes request: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
8486
}
85-
86-
# Refresh user cache after remediation
87-
try {
88-
Set-CIPPDBCacheUsers -TenantFilter $Tenant
89-
} catch {
90-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
87+
88+
# Refresh user cache after remediation only if changes were made
89+
if ($UpdateDB) {
90+
try {
91+
Set-CIPPDBCacheUsers -TenantFilter $Tenant
92+
} catch {
93+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
94+
}
9195
}
9296
} else {
9397
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'All Entra accounts for shared mailboxes are already disabled.' -sev Info

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,24 @@ function Invoke-CIPPStandardPerUserMFA {
5555
$UsersWithoutMFA = $GraphRequest | Where-Object -Property perUserMfaState -NE 'enforced' | Select-Object -Property userPrincipalName, displayName, accountEnabled, perUserMfaState
5656

5757
if ($Settings.remediate -eq $true) {
58+
$UpdateDB = $false
5859
if (($UsersWithoutMFA | Measure-Object).Count -gt 0) {
5960
try {
6061
$MFAMessage = Set-CIPPPerUserMFA -TenantFilter $Tenant -userId @($UsersWithoutMFA.userPrincipalName) -State 'enforced'
6162
Write-LogMessage -API 'Standards' -tenant $tenant -message $MFAMessage -sev Info
63+
$UpdateDB = $true
6264
} catch {
6365
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
6466
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to enforce MFA for all users: $ErrorMessage" -sev Error
6567
}
66-
67-
# Refresh user cache after remediation
68-
try {
69-
Set-CIPPDBCacheUsers -TenantFilter $Tenant
70-
} catch {
71-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
68+
69+
# Refresh user cache after remediation only if changes were made
70+
if ($UpdateDB) {
71+
try {
72+
Set-CIPPDBCacheUsers -TenantFilter $Tenant
73+
} catch {
74+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
75+
}
7276
}
7377
}
7478
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function Invoke-CIPPStandardUserPreferredLanguage {
4646
}
4747

4848
if ($Settings.remediate -eq $true) {
49+
$UpdateDB = $false
4950
if (($IncorrectUsers | Measure-Object).Count -gt 0) {
5051
try {
5152
foreach ($user in $IncorrectUsers) {
@@ -61,17 +62,20 @@ function Invoke-CIPPStandardUserPreferredLanguage {
6162
}
6263
$null = New-GraphPOSTRequest @cmdParams
6364
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Preferred language for $($user.userPrincipalName) has been set to $preferredLanguage" -sev Info
65+
$UpdateDB = $true
6466
}
6567
} catch {
6668
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
6769
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to set preferred language to $preferredLanguage for all users." -sev Error -LogData $ErrorMessage
6870
}
6971

70-
# Refresh user cache after remediation
71-
try {
72-
Set-CIPPDBCacheUsers -TenantFilter $Tenant
73-
} catch {
74-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
72+
# Refresh user cache after remediation only if changes were made
73+
if ($UpdateDB) {
74+
try {
75+
Set-CIPPDBCacheUsers -TenantFilter $Tenant
76+
} catch {
77+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh user cache after remediation: $($_.Exception.Message)" -sev Warning
78+
}
7579
}
7680
}
7781
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function Invoke-CIPPStandardcalDefault {
5050
}
5151

5252
if ($Settings.remediate -eq $true) {
53+
$UpdateDB = $false
5354
try {
5455
# Get calendar permissions from cache - this contains the calendar Identity we need
5556
$CalendarPermissions = New-CIPPDbRequest -TenantFilter $Tenant -Type 'CalendarPermissions'
@@ -89,6 +90,7 @@ function Invoke-CIPPStandardcalDefault {
8990
}
9091
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Set default calendar permission for $($Calendar.Identity) to $permissionLevel" -sev Debug
9192
$SuccessCounter++
93+
$UpdateDB = $true
9294
} catch {
9395
$ErrorCounter++
9496
$ErrorMessage = Get-CippException -Exception $_
@@ -98,11 +100,13 @@ function Invoke-CIPPStandardcalDefault {
98100

99101
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully set default calendar permissions for $SuccessCounter calendars. $ErrorCounter failed." -sev Info
100102

101-
# Refresh calendar permissions cache after remediation
102-
try {
103-
Set-CIPPDBCacheMailboxes -TenantFilter $Tenant
104-
} catch {
105-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh mailbox cache after remediation: $($_.Exception.Message)" -sev Warning
103+
# Refresh calendar permissions cache after remediation only if changes were made
104+
if ($UpdateDB) {
105+
try {
106+
Set-CIPPDBCacheMailboxes -TenantFilter $Tenant
107+
} catch {
108+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to refresh mailbox cache after remediation: $($_.Exception.Message)" -sev Warning
109+
}
106110
}
107111

108112
} catch {

0 commit comments

Comments
 (0)