Skip to content

Commit 56225b8

Browse files
Merge pull request #1576 from Ren-Roros-Digital/trycatch
chore: add try catch to CurrentState
2 parents 6da79b1 + 2cb186b commit 56225b8

File tree

112 files changed

+969
-150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+969
-150
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ function Invoke-CIPPStandardActivityBasedTimeout {
4343
Return
4444
}
4545

46-
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $Tenant
46+
try {
47+
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $Tenant
48+
}
49+
catch {
50+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
51+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the ActivityBasedTimeout state for $Tenant. Error: $ErrorMessage" -Sev Error
52+
return
53+
}
4754
$StateIsCorrect = if ($CurrentState.definition -like "*$timeout*") { $true } else { $false }
4855

4956
If ($Settings.remediate -eq $true) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ function Invoke-CIPPStandardAnonReportDisable {
3131
param($Tenant, $Settings)
3232
#$Rerun -Type Standard -Tenant $Tenant -API 'allowOTPTokens' -Settings $Settings
3333

34-
$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/admin/reportSettings' -tenantid $Tenant -AsApp $true
34+
try {
35+
$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/admin/reportSettings' -tenantid $Tenant -AsApp $true
36+
}
37+
catch {
38+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
39+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the AnonReportDisable state for $Tenant. Error: $ErrorMessage" -Sev Error
40+
return
41+
}
3542

3643
If ($Settings.remediate -eq $true) {
3744

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ function Invoke-CIPPStandardAuthMethodsPolicyMigration {
2828
#>
2929

3030
param($Tenant, $Settings)
31-
$CurrentInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/policies/authenticationMethodsPolicy' -tenantid $Tenant
31+
try {
32+
$CurrentInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/policies/authenticationMethodsPolicy' -tenantid $Tenant
33+
}
34+
catch {
35+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
36+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the AuthMethodsPolicyMigration state for $Tenant. Error: $ErrorMessage" -Sev Error
37+
return
38+
}
3239

3340
if ($null -eq $CurrentInfo) {
3441
throw "Failed to retrieve current authentication methods policy information"

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@ function Invoke-CIPPStandardAutoAddProxy {
3535
$QueueItem
3636
)
3737

38-
$Domains = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-AcceptedDomain' | Select-Object -ExpandProperty DomainName
39-
$AllMailboxes = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-Mailbox'
40-
38+
try {
39+
$Domains = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-AcceptedDomain' | Select-Object -ExpandProperty DomainName
40+
$AllMailboxes = New-ExoRequest -TenantId $Tenant -Cmdlet 'Get-Mailbox'
41+
}
42+
catch {
43+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
44+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the AutoAddProxy state for $Tenant. Error: $ErrorMessage" -Sev Error
45+
return
46+
}
47+
4148
$MissingProxies = 0
4249
foreach ($Domain in $Domains) {
4350
$ProcessMailboxes = $AllMailboxes | Where-Object {
@@ -47,15 +54,15 @@ function Invoke-CIPPStandardAutoAddProxy {
4754
}
4855
$MissingProxies += $ProcessMailboxes.Count
4956
}
50-
57+
5158
$StateIsCorrect = $MissingProxies -eq 0
52-
59+
5360
if ($Settings.report -eq $true) {
5461
$state = $StateIsCorrect ? $true : $MissingProxies
5562
Set-CIPPStandardsCompareField -FieldName 'standards.AutoAddProxy' -FieldValue $state -TenantFilter $Tenant
5663
Add-CIPPBPAField -FieldName 'AutoAddProxy' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $Tenant
5764
}
58-
65+
5966
if ($Settings.alert -eq $true) {
6067
if ($StateIsCorrect -eq $true) {
6168
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'All mailboxes have proxy addresses for all domains' -sev Info

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ function Invoke-CIPPStandardAutoExpandArchive {
3636
} #we're done.
3737
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'AutoExpandArchive'
3838

39-
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').AutoExpandingArchiveEnabled
39+
try {
40+
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').AutoExpandingArchiveEnabled
41+
}
42+
catch {
43+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
44+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the AutoExpandArchive state for $Tenant. Error: $ErrorMessage" -Sev Error
45+
return
46+
}
4047

4148
If ($Settings.remediate -eq $true) {
4249
Write-Host 'Time to remediate'

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ function Invoke-CIPPStandardBookings {
4040
# Get state value using null-coalescing operator
4141
$state = $Settings.state.value ?? $Settings.state
4242

43-
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').BookingsEnabled
43+
try {
44+
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').BookingsEnabled
45+
}
46+
catch {
47+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
48+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the Bookings state for $Tenant. Error: $ErrorMessage" -Sev Error
49+
return
50+
}
4451
$WantedState = if ($state -eq 'true') { $true } else { $false }
4552
$StateIsCorrect = if ($CurrentState -eq $WantedState) { $true } else { $false }
4653

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ function Invoke-CIPPStandardBranding {
4040

4141
try {
4242
$CurrentState = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/organization/$($TenantId.customerId)/branding/localizations/0" -tenantID $Tenant -AsApp $true
43-
} catch {
43+
}
44+
catch {
4445
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
45-
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the branding for $Tenant. This tenant might not have premium licenses available: $ErrorMessage" -Sev Error
46+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the Branding state for $Tenant. Error: $ErrorMessage" -Sev Error
47+
return
4648
}
4749

4850
# Get layoutTemplateType value using null-coalescing operator

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ function Invoke-CIPPStandardCloudMessageRecall {
4040
# Get state value using null-coalescing operator
4141
$state = $Settings.state.value ?? $Settings.state
4242

43-
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').MessageRecallEnabled
43+
try {
44+
$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').MessageRecallEnabled
45+
}
46+
catch {
47+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
48+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the CloudMessageRecall state for $Tenant. Error: $ErrorMessage" -Sev Error
49+
return
50+
}
4451
$WantedState = if ($state -eq 'true') { $true } else { $false }
4552
$StateIsCorrect = if ($CurrentState -eq $WantedState) { $true } else { $false }
4653

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ function Invoke-CIPPStandardConditionalAccessTemplate {
4040
Write-Host "We're exiting as the correct license is not present for this standard."
4141
return $true
4242
} #we're done.
43-
$AllCAPolicies = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/policies?$top=999' -tenantid $Tenant
43+
44+
try {
45+
$AllCAPolicies = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/policies?$top=999' -tenantid $Tenant
46+
}
47+
catch {
48+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
49+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the ConditionalAccessTemplate state for $Tenant. Error: $ErrorMessage" -Sev Error
50+
return
51+
}
4452

4553
if ($Settings.remediate -eq $true) {
4654
foreach ($Setting in $Settings) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ function Invoke-CIPPStandardDefaultPlatformRestrictions {
4848
try {
4949
$CurrentState = New-GraphGetRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceEnrollmentConfigurations?`$expand=assignments&orderBy=priority&`$filter=deviceEnrollmentConfigurationType eq 'SinglePlatformRestriction'" -tenantID $Tenant -AsApp $true |
5050
Select-Object -Property id, androidForWorkRestriction, androidRestriction, iosRestriction, macOSRestriction, windowsRestriction
51-
} catch {
51+
}
52+
catch {
5253
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
53-
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the DefaultPlatformRestrictions for $Tenant. This tenant might not have premium licenses available: $ErrorMessage" -Sev Error
54+
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the DefaultPlatformRestrictions state for $Tenant. Error: $ErrorMessage" -Sev Error
55+
return
5456
}
5557

5658
$StateIsCorrect = ($CurrentState.androidForWorkRestriction.platformBlocked -eq $Settings.platformAndroidForWorkBlocked) -and

0 commit comments

Comments
 (0)