Skip to content

Commit 97c8c01

Browse files
committed
better logging and fix alert being wrong
1 parent 022d8fd commit 97c8c01

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,31 @@ function Invoke-CIPPStandardPWcompanionAppAllowedState {
3030

3131
param($Tenant, $Settings)
3232

33-
$authenticatorFeaturesState = (New-GraphGetRequest -tenantid $Tenant -Uri 'https://graph.microsoft.com/beta/policies/authenticationMethodsPolicy/authenticationMethodConfigurations/microsoftAuthenticator')
34-
$authState = if ($authenticatorFeaturesState.featureSettings.companionAppAllowedState.state -eq 'enabled') { $true } else { $false }
35-
36-
33+
$AuthenticatorFeaturesState = (New-GraphGetRequest -tenantid $Tenant -Uri 'https://graph.microsoft.com/beta/policies/authenticationMethodsPolicy/authenticationMethodConfigurations/microsoftAuthenticator')
3734

3835
# Get state value using null-coalescing operator
39-
$state = $Settings.state.value ? $Settings.state.value : $settings.state
40-
$authState = if ($authenticatorFeaturesState.featureSettings.companionAppAllowedState.state -eq $state) { $true } else { $false }
36+
$CurrentState = $AuthenticatorFeaturesState.featureSettings.companionAppAllowedState.state
37+
$WantedState = $Settings.state.value ? $Settings.state.value : $settings.state
38+
$AuthStateCorrect = if ($CurrentState -eq $WantedState) { $true } else { $false }
4139

4240
# Input validation
43-
if (([string]::IsNullOrWhiteSpace($state) -or $state -eq 'Select a value') -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
41+
if (([string]::IsNullOrWhiteSpace($WantedState) -or $WantedState -eq 'Select a value') -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
4442
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'PWcompanionAppAllowedState: Invalid state parameter set' -sev Error
4543
Return
4644
}
4745

4846
If ($Settings.remediate -eq $true) {
47+
Write-Host "Remediating PWcompanionAppAllowedState for tenant $Tenant to $WantedState"
4948

50-
if ($authenticatorFeaturesState.featureSettings.companionAppAllowedState.state -eq $state) {
51-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "companionAppAllowedState is already set to the desired state of $state." -sev Info
49+
if ($AuthStateCorrect -eq $true) {
50+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "companionAppAllowedState is already set to the desired state of $WantedState." -sev Info
5251
} else {
5352
try {
5453
# Remove number matching from featureSettings because this is now Microsoft enforced and shipping it returns an error
55-
$authenticatorFeaturesState.featureSettings.PSObject.Properties.Remove('numberMatchingRequiredState')
54+
$AuthenticatorFeaturesState.featureSettings.PSObject.Properties.Remove('numberMatchingRequiredState')
5655
# Define feature body
5756
$featureBody = @{
58-
state = $state
57+
state = $WantedState
5958
includeTarget = [PSCustomObject]@{
6059
targetType = 'group'
6160
id = 'all_users'
@@ -65,33 +64,33 @@ function Invoke-CIPPStandardPWcompanionAppAllowedState {
6564
id = '00000000-0000-0000-0000-000000000000'
6665
}
6766
}
68-
$authenticatorFeaturesState.featureSettings.companionAppAllowedState = $featureBody
69-
$body = ConvertTo-Json -Depth 3 -Compress -InputObject $authenticatorFeaturesState
67+
$AuthenticatorFeaturesState.featureSettings.companionAppAllowedState = $featureBody
68+
$body = ConvertTo-Json -Depth 3 -Compress -InputObject $AuthenticatorFeaturesState
7069
$null = (New-GraphPostRequest -tenantid $Tenant -Uri 'https://graph.microsoft.com/beta/policies/authenticationMethodsPolicy/authenticationMethodConfigurations/microsoftAuthenticator' -Type patch -Body $body -ContentType 'application/json')
71-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Set companionAppAllowedState to $state." -sev Info
70+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Set companionAppAllowedState to $WantedState." -sev Info
7271
} catch {
7372
$ErrorMessage = Get-CippExceptionMessage -Exception $_
74-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to set companionAppAllowedState to $state. Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
73+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to set companionAppAllowedState to $WantedState. Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
7574
}
7675
}
7776
}
7877

7978
if ($Settings.alert -eq $true) {
8079

81-
if ($authState) {
82-
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'companionAppAllowedState is enabled.' -sev Info
80+
if ($AuthStateCorrect -eq $true) {
81+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "companionAppAllowedState is set to $WantedState." -sev Info
8382
} else {
84-
Write-StandardsAlert -message 'companionAppAllowedState is not enabled' -object $authenticatorFeaturesState -tenant $Tenant -standardName 'PWcompanionAppAllowedState' -standardId $Settings.standardId
85-
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'companionAppAllowedState is not enabled.' -sev Info
83+
Write-StandardsAlert -message "companionAppAllowedState is not set to $WantedState. Current state is $CurrentState." -object $AuthenticatorFeaturesState -tenant $Tenant -standardName 'PWcompanionAppAllowedState' -standardId $Settings.standardId
84+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "companionAppAllowedState is not set to $WantedState. Current state is $CurrentState." -sev Info
8685
}
8786
}
8887

8988
if ($Settings.report -eq $true) {
90-
Add-CIPPBPAField -FieldName 'companionAppAllowedState' -FieldValue $authState -StoreAs bool -Tenant $Tenant
91-
if ($authState) {
89+
Add-CIPPBPAField -FieldName 'companionAppAllowedState' -FieldValue $AuthStateCorrect -StoreAs bool -Tenant $Tenant
90+
if ($AuthStateCorrect -eq $true) {
9291
$FieldValue = $true
9392
} else {
94-
$FieldValue = $authenticatorFeaturesState.featureSettings.companionAppAllowedState
93+
$FieldValue = $AuthenticatorFeaturesState.featureSettings.companionAppAllowedState
9594
}
9695
Set-CIPPStandardsCompareField -FieldName 'standards.PWcompanionAppAllowedState' -FieldValue $FieldValue -Tenant $Tenant
9796
}

0 commit comments

Comments
 (0)