Skip to content

A comprehensive Windows security auditing and threat detection toolkit. Features 58 production-ready PowerShell functions organized into 14 modules for enterprise-grade security assessment, compliance validation, and incident response.

PowerShell

v1.6.2 #1: compliance percentages no longer hide unevaluable controls — NotEvaluated stays in the denominator (never compliant), Manual excluded and listed, CoveragePercentage added; CIS Summary.NotApplicable/Manual mix-up fixed; Get-ComplianceReport passes the new fields through (N/A-safe)

Okan YILDIZ4d1ad1408d80
diff --git a/Compliance/Get-ComplianceReport.ps1 b/Compliance/Get-ComplianceReport.ps1
index d3c72e2..39a898f 100644
--- a/Compliance/Get-ComplianceReport.ps1
+++ b/Compliance/Get-ComplianceReport.ps1
@@ -84,19 +84,24 @@ function Get-ComplianceReport {
Passed = $cisResult.Summary.Passed
Failed = $cisResult.Summary.Failed
NotApplicable = $cisResult.Summary.NotApplicable
+ Manual = $cisResult.Summary.Manual
+ NotEvaluated = $cisResult.Summary.NotEvaluated
+ CoveragePercentage = $cisResult.Summary.CoveragePercentage
CriticalGaps = @($cisResult.Controls | Where-Object { $_.Status -eq 'Fail' -and $_.Severity -eq 'Critical' }).Count
HighGaps = @($cisResult.Controls | Where-Object { $_.Status -eq 'Fail' -and $_.Severity -eq 'High' }).Count
MediumGaps = @($cisResult.Controls | Where-Object { $_.Status -eq 'Fail' -and $_.Severity -eq 'Medium' }).Count
Controls = $cisResult.Controls
- Status = if ($cisResult.Summary.CompliancePercentage -ge 80) { 'Pass' }
- elseif ($cisResult.Summary.CompliancePercentage -ge 60) { 'Marginal' }
+ Status = if ($null -eq $cisResult.Summary.CompliancePercentage) { 'N/A' }
+ elseif ($cisResult.Summary.CompliancePercentage -ge 80) { 'Pass' }
+ elseif ($cisResult.Summary.CompliancePercentage -ge 60) { 'Marginal' }
else { 'Fail' }
}
$report.Frameworks += $frameworkSummary
$frameworkResults += $cisResult
- Write-Host " CIS Compliance: $($cisResult.Summary.CompliancePercentage)%" -ForegroundColor $(
+ $cisComplianceDisplay = if ($null -eq $cisResult.Summary.CompliancePercentage) { 'N/A' } else { "$($cisResult.Summary.CompliancePercentage)%" }
+ Write-Host " CIS Compliance: $cisComplianceDisplay" -ForegroundColor $(
if ($cisResult.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($cisResult.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
@@ -121,19 +126,24 @@ function Get-ComplianceReport {
Passed = $nistResult.Summary.Compliant
Failed = $nistResult.Summary.NonCompliant
NotApplicable = $nistResult.Summary.NotApplicable
+ Manual = $nistResult.Summary.Manual
+ NotEvaluated = $nistResult.Summary.NotEvaluated
+ CoveragePercentage = $nistResult.Summary.CoveragePercentage
CriticalGaps = @($nistResult.Controls | Where-Object { $_.Status -eq 'NonCompliant' -and $_.Impact -eq 'Critical' }).Count
HighGaps = @($nistResult.Controls | Where-Object { $_.Status -eq 'NonCompliant' -and $_.Impact -eq 'High' }).Count
MediumGaps = @($nistResult.Controls | Where-Object { $_.Status -eq 'NonCompliant' -and $_.Impact -eq 'Medium' }).Count
Controls = $nistResult.Controls
- Status = if ($nistResult.Summary.CompliancePercentage -ge 80) { 'Pass' }
- elseif ($nistResult.Summary.CompliancePercentage -ge 60) { 'Marginal' }
+ Status = if ($null -eq $nistResult.Summary.CompliancePercentage) { 'N/A' }
+ elseif ($nistResult.Summary.CompliancePercentage -ge 80) { 'Pass' }
+ elseif ($nistResult.Summary.CompliancePercentage -ge 60) { 'Marginal' }
else { 'Fail' }
}
$report.Frameworks += $frameworkSummary
$frameworkResults += $nistResult
- Write-Host " NIST Compliance: $($nistResult.Summary.CompliancePercentage)%" -ForegroundColor $(
+ $nistComplianceDisplay = if ($null -eq $nistResult.Summary.CompliancePercentage) { 'N/A' } else { "$($nistResult.Summary.CompliancePercentage)%" }
+ Write-Host " NIST Compliance: $nistComplianceDisplay" -ForegroundColor $(
if ($nistResult.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($nistResult.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
@@ -158,19 +168,24 @@ function Get-ComplianceReport {
Passed = $pciResult.Summary.InPlace
Failed = $pciResult.Summary.NotInPlace
NotApplicable = $pciResult.Summary.NotApplicable
+ Manual = $pciResult.Summary.Manual
+ NotEvaluated = $pciResult.Summary.NotEvaluated
+ CoveragePercentage = $pciResult.Summary.CoveragePercentage
CriticalGaps = @($pciResult.Requirements | Where-Object { $_.Status -eq 'NotInPlace' -and $_.Priority -eq 'Critical' }).Count
HighGaps = @($pciResult.Requirements | Where-Object { $_.Status -eq 'NotInPlace' -and $_.Priority -eq 'High' }).Count
MediumGaps = @($pciResult.Requirements | Where-Object { $_.Status -eq 'NotInPlace' -and $_.Priority -eq 'Medium' }).Count
Controls = $pciResult.Requirements
- Status = if ($pciResult.Summary.CompliancePercentage -eq 100) { 'Pass' }
- elseif ($pciResult.Summary.CompliancePercentage -ge 80) { 'Marginal' }
+ Status = if ($null -eq $pciResult.Summary.CompliancePercentage) { 'N/A' }
+ elseif ($pciResult.Summary.CompliancePercentage -eq 100) { 'Pass' }
+ elseif ($pciResult.Summary.CompliancePercentage -ge 80) { 'Marginal' }
else { 'Fail' }
}
$report.Frameworks += $frameworkSummary
$frameworkResults += $pciResult
- Write-Host " PCI-DSS Compliance: $($pciResult.Summary.CompliancePercentage)%" -ForegroundColor $(
+ $pciComplianceDisplay = if ($null -eq $pciResult.Summary.CompliancePercentage) { 'N/A' } else { "$($pciResult.Summary.CompliancePercentage)%" }
+ Write-Host " PCI-DSS Compliance: $pciComplianceDisplay" -ForegroundColor $(
if ($pciResult.Summary.CompliancePercentage -eq 100) { 'Green' }
elseif ($pciResult.Summary.CompliancePercentage -ge 80) { 'Yellow' }
else { 'Red' }
@@ -374,7 +389,7 @@ function Get-ComplianceReport {
# Export CSV (Summary)
if ($Format -in @('CSV', 'All')) {
$csvPath = Join-Path $OutputPath "$reportName.csv"
- $report.Frameworks | Export-Csv $csvPath -NoTypeInformation
+ $report.Frameworks | ConvertTo-CsvSafeObject | Export-Csv $csvPath -NoTypeInformation
Write-Host " CSV report saved: $csvPath" -ForegroundColor Green
}
@@ -411,7 +426,8 @@ function Get-ComplianceReport {
Write-Host "`nFramework Results:" -ForegroundColor Cyan
foreach ($framework in $report.Frameworks) {
- Write-Host " $($framework.Framework): $($framework.CompliancePercentage)% [$($framework.Status)]" -ForegroundColor $(
+ $frameworkComplianceDisplay = if ($null -eq $framework.CompliancePercentage) { 'N/A' } else { "$($framework.CompliancePercentage)%" }
+ Write-Host " $($framework.Framework): $frameworkComplianceDisplay [$($framework.Status)]" -ForegroundColor $(
if ($framework.Status -eq 'Pass') { 'Green' }
elseif ($framework.Status -eq 'Marginal') { 'Yellow' }
else { 'Red' }
diff --git a/Compliance/Test-CISBenchmark.ps1 b/Compliance/Test-CISBenchmark.ps1
index 32025ba..6542e9c 100644
--- a/Compliance/Test-CISBenchmark.ps1
+++ b/Compliance/Test-CISBenchmark.ps1
@@ -48,7 +48,11 @@
Passed = 0
Failed = 0
NotApplicable = 0
+ Manual = 0
+ NotEvaluated = 0
+ EvaluatedControls = 0
CompliancePercentage = 0
+ CoveragePercentage = 0
}
Recommendations = @()
}
@@ -131,9 +135,9 @@
try {
# Export security policy. secedit is Windows-only; when it is
- # missing or the export fails, emit the controls as Manual instead
- # of silently dropping them (the per-setting 'if ($value)' guards
- # below used to skip controls with no warning).
+ # missing or the export fails, emit the controls as NotEvaluated
+ # instead of silently dropping them (the per-setting 'if ($value)'
+ # guards below used to skip controls with no warning).
$seceditCmd = Get-Command 'secedit.exe' -ErrorAction SilentlyContinue
if ($null -eq $seceditCmd) { $seceditCmd = Get-Command 'secedit' -ErrorAction SilentlyContinue }
@@ -173,7 +177,7 @@
Category = 'PasswordPolicy'
Expected = $spec.Expected
Actual = 'Unable to evaluate — secedit export unavailable'
- Status = 'Manual'
+ Status = 'NotEvaluated'
Severity = $spec.Severity
Remediation = $spec.Remediation
}
@@ -189,7 +193,7 @@
Category = 'PasswordPolicy'
Expected = '24 or more passwords'
Actual = if ($null -ne $passwordHistory) { "$passwordHistory passwords" } else { 'Unable to evaluate — setting missing from secedit export' }
- Status = if ($null -eq $passwordHistory) { 'Manual' } elseif ([int]$passwordHistory -ge 24) { 'Pass' } else { 'Fail' }
+ Status = if ($null -eq $passwordHistory) { 'NotEvaluated' } elseif ([int]$passwordHistory -ge 24) { 'Pass' } else { 'Fail' }
Severity = 'High'
Remediation = 'Set to 24 or more passwords remembered'
}
@@ -203,7 +207,7 @@
Category = 'PasswordPolicy'
Expected = '365 or fewer days (but not 0)'
Actual = if ($null -ne $maxPasswordAge) { "$maxPasswordAge days" } else { 'Unable to evaluate — setting missing from secedit export' }
- Status = if ($null -eq $maxPasswordAge) { 'Manual' } elseif ([int]$maxPasswordAge -le 365 -and [int]$maxPasswordAge -gt 0) { 'Pass' } else { 'Fail' }
+ Status = if ($null -eq $maxPasswordAge) { 'NotEvaluated' } elseif ([int]$maxPasswordAge -le 365 -and [int]$maxPasswordAge -gt 0) { 'Pass' } else { 'Fail' }
Severity = 'High'
Remediation = 'Set to 365 or fewer days'
}
@@ -217,7 +221,7 @@
Category = 'PasswordPolicy'
Expected = '1 or more days'
Actual = if ($null -ne $minPasswordAge) { "$minPasswordAge days" } else { 'Unable to evaluate — setting missing from secedit export' }
- Status = if ($null -eq $minPasswordAge) { 'Manual' } elseif ([int]$minPasswordAge -ge 1) { 'Pass' } else { 'Fail' }
+ Status = if ($null -eq $minPasswordAge) { 'NotEvaluated' } elseif ([int]$minPasswordAge -ge 1) { 'Pass' } else { 'Fail' }
Severity = 'Medium'
Remediation = 'Set to 1 or more days'
}
@@ -231,7 +235,7 @@
Category = 'PasswordPolicy'
Expected = '14 or more characters'
Actual = if ($null -ne $minPasswordLength) { "$minPasswordLength characters" } else { 'Unable to evaluate — setting missing from secedit export' }
- Status = if ($null -eq $minPasswordLength) { 'Manual' } elseif ([int]$minPasswordLength -ge 14) { 'Pass' } else { 'Fail' }
+ Status = if ($null -eq $minPasswordLength) { 'NotEvaluated' } elseif ([int]$minPasswordLength -ge 14) { 'Pass' } else { 'Fail' }
Severity = 'High'
Remediation = 'Set to 14 or more characters'
}
@@ -246,7 +250,7 @@
Expected = 'Enabled'
Actual = if ($null -eq $passwordComplexity) { 'Unable to evaluate — setting missing from secedit export' }
elseif ($passwordComplexity -eq '1') { 'Enabled' } else { 'Disabled' }
- Status = if ($null -eq $passwordComplexity) { 'Manual' } elseif ($passwordComplexity -eq '1') { 'Pass' } else { 'Fail' }
+ Status = if ($null -eq $passwordComplexity) { 'NotEvaluated' } elseif ($passwordComplexity -eq '1') { 'Pass' } else { 'Fail' }
Severity = 'Critical'
Remediation = 'Enable password complexity requirements'
}
@@ -281,7 +285,7 @@
elseif ($credentialValidation.Success -and $credentialValidation.Failure) { 'Success and Failure' }
elseif ($credentialValidation.Success) { 'Success' }
elseif ($credentialValidation.Failure) { 'Failure' } else { 'No Auditing' }
- Status = if ($null -eq $credentialValidation -or $null -eq $credentialValidation.Value) { 'Manual' }
+ Status = if ($null -eq $credentialValidation -or $null -eq $credentialValidation.Value) { 'NotEvaluated' }
elseif ($credentialValidation.Value -eq 3) { 'Pass' } else { 'Fail' }
Severity = 'High'
Remediation = 'Configure to audit Success and Failure'
@@ -305,7 +309,7 @@
elseif ($appGroupManagement.Success -and $appGroupManagement.Failure) { 'Success and Failure' }
elseif ($appGroupManagement.Success) { 'Success' }
elseif ($appGroupManagement.Failure) { 'Failure' } else { 'No Auditing' }
- Status = if ($null -eq $appGroupManagement -or $null -eq $appGroupManagement.Value) { 'Manual' }
+ Status = if ($null -eq $appGroupManagement -or $null -eq $appGroupManagement.Value) { 'NotEvaluated' }
elseif ($appGroupManagement.Value -eq 3) { 'Pass' } else { 'Fail' }
Severity = 'Medium'
Remediation = 'Configure to audit Success and Failure'
@@ -327,7 +331,7 @@
Expected = 'Success'
Actual = if ($null -eq $accountLockout -or $null -eq $accountLockout.Value) { 'Unable to determine' }
elseif ($accountLockout.Value -ge 1 -and $accountLockout.Value -le 3) { 'Configured' } else { 'Not Configured' }
- Status = if ($null -eq $accountLockout -or $null -eq $accountLockout.Value) { 'Manual' }
+ Status = if ($null -eq $accountLockout -or $null -eq $accountLockout.Value) { 'NotEvaluated' }
elseif ($accountLockout.Value -ge 1 -and $accountLockout.Value -le 3) { 'Pass' } else { 'Fail' }
Severity = 'Medium'
Remediation = 'Configure to audit Success'
@@ -354,7 +358,7 @@
Expected = 'Disabled'
Actual = if (-not $adminKnown) { 'Unable to determine' }
elseif ($adminUser.Enabled) { 'Enabled' } else { 'Disabled' }
- Status = if (-not $adminKnown) { 'Manual' }
+ Status = if (-not $adminKnown) { 'NotEvaluated' }
elseif ($adminUser.Enabled) { 'Fail' } else { 'Pass' }
Severity = 'High'
Remediation = 'Disable the built-in Administrator account'
@@ -371,7 +375,7 @@
Expected = 'Disabled'
Actual = if (-not $guestKnown) { 'Unable to determine' }
elseif ($guestUser.Enabled) { 'Enabled' } else { 'Disabled' }
- Status = if (-not $guestKnown) { 'Manual' }
+ Status = if (-not $guestKnown) { 'NotEvaluated' }
elseif ($guestUser.Enabled) { 'Fail' } else { 'Pass' }
Severity = 'High'
Remediation = 'Disable the Guest account'
@@ -437,7 +441,7 @@
Category = 'WindowsFirewall'
Expected = 'On'
Actual = 'Unable to evaluate — Get-NetFirewallProfile unavailable or returned no profiles'
- Status = 'Manual'
+ Status = 'NotEvaluated'
Severity = 'Critical'
Remediation = 'Verify Windows Firewall is enabled for all profiles'
}
@@ -518,15 +522,31 @@
$results.Summary.TotalControls = $results.Controls.Count
$results.Summary.Passed = @($results.Controls | Where-Object { $_.Status -eq 'Pass' }).Count
$results.Summary.Failed = @($results.Controls | Where-Object { $_.Status -eq 'Fail' }).Count
- $results.Summary.NotApplicable = @($results.Controls | Where-Object { $_.Status -eq 'Manual' }).Count
-
- if ($results.Summary.TotalControls -gt 0) {
- $testableControls = $results.Summary.TotalControls - $results.Summary.NotApplicable
- if ($testableControls -gt 0) {
- $results.Summary.CompliancePercentage = [math]::Round(
- ($results.Summary.Passed / $testableControls) * 100, 2
- )
- }
+ $results.Summary.NotApplicable = @($results.Controls | Where-Object { $_.Status -eq 'NotApplicable' }).Count
+ $results.Summary.Manual = @($results.Controls | Where-Object { $_.Status -eq 'Manual' }).Count
+ $results.Summary.NotEvaluated = @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' }).Count
+
+ # NotEvaluated stays in the denominator and never counts as compliant;
+ # only NotApplicable and Manual leave the denominator.
+ $scopedControls = $results.Summary.TotalControls - $results.Summary.NotApplicable - $results.Summary.Manual
+ $evaluatedControls = $scopedControls - $results.Summary.NotEvaluated
+ $results.Summary.EvaluatedControls = $evaluatedControls
+ if ($scopedControls -gt 0) {
+ $results.Summary.CompliancePercentage = [math]::Round(
+ ($results.Summary.Passed / $scopedControls) * 100, 2
+ )
+ $results.Summary.CoveragePercentage = [math]::Round(
+ ($evaluatedControls / $scopedControls) * 100, 2
+ )
+ }
+ else {
+ $results.Summary.CompliancePercentage = $null
+ $results.Summary.CoveragePercentage = $null
+ }
+
+ if ($results.Summary.NotEvaluated -gt 0) {
+ $notEvaluatedIds = @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' } | ForEach-Object { $_.ControlID }) -join ', '
+ Write-Warning "$($results.Summary.NotEvaluated) control(s) could not be evaluated and are counted as not compliant: $notEvaluatedIds"
}
# Generate recommendations - FIX: Use @() to ensure array
@@ -541,6 +561,10 @@
if ($failedHigh.Count -gt 0) {
$results.Recommendations += "HIGH: $($failedHigh.Count) high-severity control(s) failed"
}
+
+ foreach ($notEvaluatedControl in @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' })) {
+ $results.Recommendations += "Could not evaluate $($notEvaluatedControl.ControlID): run elevated or install the required feature/module"
+ }
if ($results.Summary.CompliancePercentage -lt 70) {
$results.Recommendations += "Compliance below 70% - comprehensive security review required"
@@ -557,18 +581,23 @@
}
end {
+ $complianceDisplay = if ($null -eq $results.Summary.CompliancePercentage) { 'N/A' } else { "$($results.Summary.CompliancePercentage)%" }
+ $coverageDisplay = if ($null -eq $results.Summary.CoveragePercentage) { 'N/A' } else { "$($results.Summary.CoveragePercentage)%" }
+
Write-Host "`n=== COMPLIANCE TEST COMPLETE ===" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
)
-
+
Write-Host "`nCompliance Summary:" -ForegroundColor Cyan
Write-Host " Total Controls Tested: $($results.Summary.TotalControls)" -ForegroundColor White
Write-Host " Passed: $($results.Summary.Passed)" -ForegroundColor Green
Write-Host " Failed: $($results.Summary.Failed)" -ForegroundColor Red
- Write-Host " Manual Review: $($results.Summary.NotApplicable)" -ForegroundColor Yellow
- Write-Host " Compliance: $($results.Summary.CompliancePercentage)%" -ForegroundColor $(
+ Write-Host " Not Applicable: $($results.Summary.NotApplicable)" -ForegroundColor Gray
+ Write-Host " Manual Review: $($results.Summary.Manual)" -ForegroundColor Yellow
+ Write-Host " Not Evaluated: $($results.Summary.NotEvaluated)" -ForegroundColor Yellow
+ Write-Host " Compliance: $complianceDisplay (coverage $coverageDisplay, $($results.Summary.NotEvaluated) not evaluated)" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
diff --git a/Compliance/Test-NISTCompliance.ps1 b/Compliance/Test-NISTCompliance.ps1
index 8aaba2e..c1001d4 100644
--- a/Compliance/Test-NISTCompliance.ps1
+++ b/Compliance/Test-NISTCompliance.ps1
@@ -51,7 +51,11 @@
NonCompliant = 0
PartiallyCompliant = 0
NotApplicable = 0
+ Manual = 0
+ NotEvaluated = 0
+ EvaluatedControls = 0
CompliancePercentage = 0
+ CoveragePercentage = 0
}
Recommendations = @()
}
@@ -113,7 +117,7 @@
Description = 'Guest and default Administrator accounts should be disabled'
Expected = 'Guest and Administrator accounts disabled'
Actual = "Guest: $(if(-not $guestKnown){'Unknown'}elseif($guestDisabled){'Disabled'}else{'Enabled'}), Admin: $(if(-not $adminKnown){'Unknown'}elseif($adminDisabled){'Disabled'}else{'Enabled'})"
- Status = if (-not $guestKnown -or -not $adminKnown) { 'NotApplicable' }
+ Status = if (-not $guestKnown -or -not $adminKnown) { 'NotEvaluated' }
elseif ($guestDisabled -and $adminDisabled) { 'Compliant' }
elseif ($guestDisabled -or $adminDisabled) { 'PartiallyCompliant' }
else { 'NonCompliant' }
@@ -138,7 +142,7 @@
Actual = if ($null -eq $lockoutThreshold) { 'Unable to determine' }
elseif ($lockoutThreshold -eq 0) { 'Lockout disabled (0)' }
else { "$lockoutThreshold attempts" }
- Status = if ($null -eq $lockoutThreshold) { 'NotApplicable' }
+ Status = if ($null -eq $lockoutThreshold) { 'NotEvaluated' }
elseif ($lockoutThreshold -ge 1 -and $lockoutThreshold -le 10) { 'Compliant' }
else { 'NonCompliant' }
Impact = 'High'
@@ -213,7 +217,7 @@
Expected = 'Audit policies configured'
Actual = if ($null -eq $logonAuditValue) { 'Unable to determine' }
elseif ($logonAuditValue -ge 1 -and $logonAuditValue -le 3) { "Configured" } else { "Not configured" }
- Status = if ($null -eq $logonAuditValue) { 'NotApplicable' }
+ Status = if ($null -eq $logonAuditValue) { 'NotEvaluated' }
elseif ($logonAuditValue -ge 1 -and $logonAuditValue -le 3) { 'Compliant' } else { 'NonCompliant' }
Impact = 'High'
Remediation = 'Enable audit policies for security events'
@@ -236,7 +240,7 @@
Expected = 'Security log size >= 100MB'
Actual = if (-not $winEventAvailable) { 'Unable to evaluate — Get-WinEvent unavailable' }
else { "$([math]::Round($eventLogSize/1MB)) MB" }
- Status = if (-not $winEventAvailable) { 'NotApplicable' }
+ Status = if (-not $winEventAvailable) { 'NotEvaluated' }
elseif ($eventLogSize -ge 100MB) { 'Compliant' } else { 'NonCompliant' }
Impact = 'Medium'
Remediation = 'Increase Security event log size to at least 100MB'
@@ -258,7 +262,7 @@
Description = 'Audit logs must be protected from unauthorized access'
Expected = 'Security log protected from normal users'
Actual = 'Unable to read ACL'
- Status = 'NotApplicable'
+ Status = 'NotEvaluated'
Impact = 'High'
Remediation = 'Restrict Security event log access to administrators only'
}
@@ -298,7 +302,7 @@
Expected = 'Process creation auditing enabled'
Actual = if ($null -eq $processAuditEntry -or $null -eq $processAuditEntry.Value) { 'Unable to determine' }
elseif ($processAuditEntry.Success) { "Enabled" } else { "Disabled" }
- Status = if ($null -eq $processAuditEntry -or $null -eq $processAuditEntry.Value) { 'NotApplicable' }
+ Status = if ($null -eq $processAuditEntry -or $null -eq $processAuditEntry.Value) { 'NotEvaluated' }
elseif ($processAuditEntry.Success) { 'Compliant' } else { 'NonCompliant' }
Impact = 'Medium'
Remediation = 'Enable process creation auditing'
@@ -327,7 +331,7 @@
Expected = 'Minimum password length >= 14 characters'
Actual = if ($null -eq $minPasswordLength) { 'Unable to determine' }
else { "$minPasswordLength characters" }
- Status = if ($null -eq $minPasswordLength) { 'NotApplicable' }
+ Status = if ($null -eq $minPasswordLength) { 'NotEvaluated' }
elseif ($minPasswordLength -ge 14) { 'Compliant' } else { 'NonCompliant' }
Impact = 'High'
Remediation = 'Set minimum password length to 14 characters'
@@ -345,7 +349,7 @@
Expected = 'Maximum password age <= 60 days'
Actual = if ($null -eq $maxAgeDays) { 'Unable to determine' }
elseif ($maxAgeDays -eq -1) { 'Never expires' } else { "$maxAgeDays days" }
- Status = if ($null -eq $maxAgeDays) { 'NotApplicable' }
+ Status = if ($null -eq $maxAgeDays) { 'NotEvaluated' }
elseif ($maxAgeDays -gt 0 -and $maxAgeDays -le 60) { 'Compliant' } else { 'NonCompliant' }
Impact = 'High'
Remediation = 'Set maximum password age to 60 days or less'
@@ -399,7 +403,8 @@
Actual = if (-not $firewallCmdletAvailable) { 'Unable to evaluate — Get-NetFirewallProfile unavailable' }
elseif ($null -eq $firewallProfiles) { "Unable to retrieve firewall profiles" }
elseif ($allFirewallsEnabled) { "All enabled" } else { "Some disabled" }
- Status = if (-not $firewallCmdletAvailable) { 'NotApplicable' }
+ Status = if (-not $firewallCmdletAvailable) { 'NotEvaluated' }
+ elseif ($null -eq $firewallProfiles) { 'NotEvaluated' }
elseif ($allFirewallsEnabled) { 'Compliant' } else { 'NonCompliant' }
Impact = 'Critical'
Remediation = 'Enable Windows Firewall for all profiles'
@@ -442,7 +447,7 @@
Expected = 'BitLocker enabled'
Actual = if (-not $bitlockerAvailable) { 'Unable to evaluate — Get-BitLockerVolume unavailable' }
elseif ($bitlockerEnabled) { "Enabled" } else { "Disabled" }
- Status = if (-not $bitlockerAvailable) { 'NotApplicable' }
+ Status = if (-not $bitlockerAvailable) { 'NotEvaluated' }
elseif ($bitlockerEnabled) { 'Compliant' } else { 'NonCompliant' }
Impact = 'High'
Remediation = 'Enable BitLocker encryption'
@@ -478,7 +483,7 @@
Expected = 'Windows Defender enabled'
Actual = if (-not $mpStatusAvailable) { 'Unable to evaluate — Get-MpComputerStatus unavailable' }
elseif ($defenderEnabled) { "Enabled" } else { "Disabled" }
- Status = if (-not $mpStatusAvailable) { 'NotApplicable' }
+ Status = if (-not $mpStatusAvailable) { 'NotEvaluated' }
elseif ($defenderEnabled) { 'Compliant' } else { 'NonCompliant' }
Impact = 'Critical'
Remediation = 'Enable Windows Defender antivirus'
@@ -505,7 +510,7 @@
Expected = 'No unnecessary services running'
Actual = if (-not $serviceCmdletAvailable) { 'Unable to evaluate — Get-Service unavailable' }
elseif ($unnecessaryServices.Count -eq 0) { "Clean" } else { "$($unnecessaryServices.Count) unnecessary services" }
- Status = if (-not $serviceCmdletAvailable) { 'NotApplicable' }
+ Status = if (-not $serviceCmdletAvailable) { 'NotEvaluated' }
elseif ($unnecessaryServices.Count -eq 0) { 'Compliant' } else { 'NonCompliant' }
Impact = 'Medium'
Remediation = 'Disable unnecessary services (Telnet, SNMP, SSDP)'
@@ -520,7 +525,7 @@
Description = 'Maintain accurate inventory of system components'
Expected = 'Inventory maintained'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Impact = 'Low'
Remediation = 'Implement automated inventory management'
}
@@ -537,11 +542,27 @@
$results.Summary.NonCompliant = @($results.Controls | Where-Object { $_.Status -eq 'NonCompliant' }).Count
$results.Summary.PartiallyCompliant = @($results.Controls | Where-Object { $_.Status -eq 'PartiallyCompliant' }).Count
$results.Summary.NotApplicable = @($results.Controls | Where-Object { $_.Status -eq 'NotApplicable' }).Count
-
- $testableControls = $results.Summary.TotalControls - $results.Summary.NotApplicable
- if ($testableControls -gt 0) {
+ $results.Summary.Manual = @($results.Controls | Where-Object { $_.Status -eq 'Manual' }).Count
+ $results.Summary.NotEvaluated = @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' }).Count
+
+ # NotEvaluated stays in the denominator and never counts as compliant;
+ # only NotApplicable and Manual leave the denominator.
+ $scopedControls = $results.Summary.TotalControls - $results.Summary.NotApplicable - $results.Summary.Manual
+ $evaluatedControls = $scopedControls - $results.Summary.NotEvaluated
+ $results.Summary.EvaluatedControls = $evaluatedControls
+ if ($scopedControls -gt 0) {
$compliantCount = $results.Summary.Compliant + ($results.Summary.PartiallyCompliant * 0.5)
- $results.Summary.CompliancePercentage = [math]::Round(($compliantCount / $testableControls) * 100, 2)
+ $results.Summary.CompliancePercentage = [math]::Round(($compliantCount / $scopedControls) * 100, 2)
+ $results.Summary.CoveragePercentage = [math]::Round(($evaluatedControls / $scopedControls) * 100, 2)
+ }
+ else {
+ $results.Summary.CompliancePercentage = $null
+ $results.Summary.CoveragePercentage = $null
+ }
+
+ if ($results.Summary.NotEvaluated -gt 0) {
+ $notEvaluatedIds = @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' } | ForEach-Object { $_.ControlID }) -join ', '
+ Write-Warning "$($results.Summary.NotEvaluated) control(s) could not be evaluated and are counted as not compliant: $notEvaluatedIds"
}
# Generate recommendations - FIX: All wrapped in @()
@@ -556,6 +577,10 @@
if ($highFailed.Count -gt 0) {
$results.Recommendations += "HIGH: $($highFailed.Count) high-impact control(s) non-compliant"
}
+
+ foreach ($notEvaluatedControl in @($results.Controls | Where-Object { $_.Status -eq 'NotEvaluated' })) {
+ $results.Recommendations += "Could not evaluate $($notEvaluatedControl.ControlID): run elevated or install the required feature/module"
+ }
if ($results.Summary.CompliancePercentage -lt 80) {
$results.Recommendations += "Compliance below 80% - implement remediation plan"
@@ -572,19 +597,24 @@
}
end {
+ $complianceDisplay = if ($null -eq $results.Summary.CompliancePercentage) { 'N/A' } else { "$($results.Summary.CompliancePercentage)%" }
+ $coverageDisplay = if ($null -eq $results.Summary.CoveragePercentage) { 'N/A' } else { "$($results.Summary.CoveragePercentage)%" }
+
Write-Host "`n=== COMPLIANCE TEST COMPLETE ===" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
)
-
+
Write-Host "`nCompliance Summary:" -ForegroundColor Cyan
Write-Host " Total Controls Tested: $($results.Summary.TotalControls)" -ForegroundColor White
Write-Host " Compliant: $($results.Summary.Compliant)" -ForegroundColor Green
Write-Host " Non-Compliant: $($results.Summary.NonCompliant)" -ForegroundColor Red
Write-Host " Partially Compliant: $($results.Summary.PartiallyCompliant)" -ForegroundColor Yellow
Write-Host " Not Applicable: $($results.Summary.NotApplicable)" -ForegroundColor Gray
- Write-Host " Compliance: $($results.Summary.CompliancePercentage)%" -ForegroundColor $(
+ Write-Host " Manual: $($results.Summary.Manual)" -ForegroundColor Yellow
+ Write-Host " Not Evaluated: $($results.Summary.NotEvaluated)" -ForegroundColor Yellow
+ Write-Host " Compliance: $complianceDisplay (coverage $coverageDisplay, $($results.Summary.NotEvaluated) not evaluated)" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -ge 80) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 60) { 'Yellow' }
else { 'Red' }
diff --git a/Compliance/Test-PciDssCompliance.ps1 b/Compliance/Test-PciDssCompliance.ps1
index d1b825b..c1396c4 100644
--- a/Compliance/Test-PciDssCompliance.ps1
+++ b/Compliance/Test-PciDssCompliance.ps1
@@ -50,7 +50,11 @@
InPlace = 0
NotInPlace = 0
NotApplicable = 0
+ Manual = 0
+ NotEvaluated = 0
+ EvaluatedControls = 0
CompliancePercentage = 0
+ CoveragePercentage = 0
}
Recommendations = @()
}
@@ -117,7 +121,8 @@
Actual = if (-not $firewallCmdletAvailable) { 'Unable to evaluate — Get-NetFirewallProfile unavailable' }
elseif ($null -eq $firewallProfiles) { "Unable to retrieve firewall profiles" }
elseif ($allFirewallsEnabled) { "All enabled" } else { "Some disabled" }
- Status = if (-not $firewallCmdletAvailable) { 'NotApplicable' }
+ Status = if (-not $firewallCmdletAvailable) { 'NotEvaluated' }
+ elseif ($null -eq $firewallProfiles) { 'NotEvaluated' }
elseif ($allFirewallsEnabled) { 'InPlace' } else { 'NotInPlace' }
Priority = 'Critical'
Remediation = 'Enable all Windows Firewall profiles (Domain, Private, Public)'
@@ -136,8 +141,9 @@
Actual = if (-not $firewallCmdletAvailable) { 'Unable to evaluate — Get-NetFirewallProfile unavailable' }
elseif ($null -eq $firewallProfiles) { "Unable to retrieve firewall profiles" }
elseif ($defaultInboundBlock.Count -eq 0) { "All blocked" } else { "$($defaultInboundBlock.Count) profiles allow" }
- Status = if (-not $firewallCmdletAvailable) { 'NotApplicable' }
- elseif (($null -ne $firewallProfiles) -and $defaultInboundBlock.Count -eq 0) { 'InPlace' } else { 'NotInPlace' }
+ Status = if (-not $firewallCmdletAvailable) { 'NotEvaluated' }
+ elseif ($null -eq $firewallProfiles) { 'NotEvaluated' }
+ elseif ($defaultInboundBlock.Count -eq 0) { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Set default inbound action to Block for all profiles'
}
@@ -154,7 +160,8 @@
Expected = 'Public profile enabled and blocking inbound'
Actual = if (-not $firewallCmdletAvailable) { 'Unable to evaluate — Get-NetFirewallProfile unavailable' }
elseif ($publicProfile) { "Enabled: $($publicProfile.Enabled), Inbound: $($publicProfile.DefaultInboundAction)" } else { "Public profile not found" }
- Status = if (-not $firewallCmdletAvailable) { 'NotApplicable' }
+ Status = if (-not $firewallCmdletAvailable) { 'NotEvaluated' }
+ elseif ($null -eq $firewallProfiles) { 'NotEvaluated' }
elseif ($publicProfile -and $publicProfile.Enabled -and $publicProfile.DefaultInboundAction -eq 'Block') { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Enable Public profile with default inbound block'
@@ -179,7 +186,7 @@
Description = 'Sensitive authentication data must not be stored'
Expected = 'No card data stored (manual verification)'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'Critical'
Remediation = 'Ensure no full track data, CVV2, or PIN blocks are stored'
}
@@ -203,7 +210,7 @@
Expected = 'Disk encryption enabled (BitLocker)'
Actual = if (-not $bitlockerAvailable) { 'Unable to evaluate — Get-BitLockerVolume unavailable' }
elseif ($bitlockerEnabled) { "Enabled" } else { "Disabled" }
- Status = if (-not $bitlockerAvailable) { 'NotApplicable' }
+ Status = if (-not $bitlockerAvailable) { 'NotEvaluated' }
elseif ($bitlockerEnabled) { 'InPlace' } else { 'NotInPlace' }
Priority = 'Critical'
Remediation = 'Enable BitLocker or third-party disk encryption'
@@ -218,7 +225,7 @@
Description = 'Cryptographic keys must be managed securely'
Expected = 'Key management procedures documented'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'High'
Remediation = 'Document and implement key management procedures'
}
@@ -247,7 +254,7 @@
Expected = 'Guest account disabled'
Actual = if (-not $guestKnown) { 'Unable to determine (insufficient privileges or unsupported platform)' }
elseif ($guestDisabled) { "Disabled" } else { "Enabled" }
- Status = if (-not $guestKnown) { 'NotApplicable' }
+ Status = if (-not $guestKnown) { 'NotEvaluated' }
elseif ($guestDisabled) { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Disable Guest and other unnecessary accounts'
@@ -266,7 +273,7 @@
Expected = 'Minimum 12 characters (or 8 if MFA)'
Actual = if ($null -eq $minPasswordLength) { 'Unable to determine (insufficient privileges or unsupported platform)' }
else { "$minPasswordLength characters" }
- Status = if ($null -eq $minPasswordLength) { 'NotApplicable' }
+ Status = if ($null -eq $minPasswordLength) { 'NotEvaluated' }
elseif ($minPasswordLength -ge 12) { 'InPlace' } else { 'NotInPlace' }
Priority = 'Critical'
Remediation = 'Set minimum password length to 12 characters'
@@ -284,7 +291,7 @@
Expected = '90 days or less'
Actual = if ($null -eq $maxAgeDays) { 'Unable to determine (insufficient privileges or unsupported platform)' }
elseif ($maxAgeDays -eq -1) { 'Never expires' } else { "$maxAgeDays days" }
- Status = if ($null -eq $maxAgeDays) { 'NotApplicable' }
+ Status = if ($null -eq $maxAgeDays) { 'NotEvaluated' }
elseif ($maxAgeDays -gt 0 -and $maxAgeDays -le 90) { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Set maximum password age to 90 days'
@@ -299,7 +306,7 @@
Description = 'MFA required for all access to CDE'
Expected = 'MFA enabled for administrators'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'Critical'
Remediation = 'Implement MFA (Windows Hello, Smart Card, or third-party)'
}
@@ -332,7 +339,7 @@
Expected = 'Logon auditing: Success and Failure'
Actual = if ($null -eq $logonAuditValue) { 'Unable to determine (insufficient privileges or unsupported platform)' }
elseif ($logonAuditValue -eq 3) { "Enabled" } else { "Not configured" }
- Status = if ($null -eq $logonAuditValue) { 'NotApplicable' }
+ Status = if ($null -eq $logonAuditValue) { 'NotEvaluated' }
elseif ($logonAuditValue -eq 3) { 'InPlace' } else { 'NotInPlace' }
Priority = 'Critical'
Remediation = 'Enable audit policy for Logon events'
@@ -356,7 +363,7 @@
Expected = 'File system auditing enabled'
Actual = if ($null -eq $objectAuditValue) { 'Unable to determine (insufficient privileges or unsupported platform)' }
elseif ($objectAuditValue -ge 1 -and $objectAuditValue -le 3) { "Enabled" } else { "Not configured" }
- Status = if ($null -eq $objectAuditValue) { 'NotApplicable' }
+ Status = if ($null -eq $objectAuditValue) { 'NotEvaluated' }
elseif ($objectAuditValue -ge 1 -and $objectAuditValue -le 3) { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Enable file system object access auditing'
@@ -379,7 +386,7 @@
Expected = 'Security log >= 100MB'
Actual = if (-not $winEventAvailable) { 'Unable to evaluate — Get-WinEvent unavailable' }
else { "$([math]::Round($eventLogSize/1MB)) MB" }
- Status = if (-not $winEventAvailable) { 'NotApplicable' }
+ Status = if (-not $winEventAvailable) { 'NotEvaluated' }
elseif ($eventLogSize -ge 100MB) { 'InPlace' } else { 'NotInPlace' }
Priority = 'High'
Remediation = 'Increase Security log size to at least 100MB'
@@ -403,7 +410,7 @@
Expected = 'Windows Time service running'
Actual = if (-not $serviceCmdletAvailable) { 'Unable to evaluate — Get-Service unavailable' }
elseif ($w32timeService) { $w32timeService.Status } else { "Not found" }
- Status = if (-not $serviceCmdletAvailable) { 'NotApplicable' }
+ Status = if (-not $serviceCmdletAvailable) { 'NotEvaluated' }
elseif ($w32timeService -and $w32timeService.Status -eq 'Running') { 'InPlace' } else { 'NotInPlace' }
Priority = 'Medium'
Remediation = 'Ensure Windows Time service is running and configured'
@@ -438,7 +445,7 @@
Expected = 'Antivirus enabled'
Actual = if (-not $mpStatusAvailable) { 'Unable to evaluate — Get-MpComputerStatus unavailable' }
elseif ($defenderEnabled) { "Windows Defender enabled" } else { "Disabled" }
- Status = if (-not $mpStatusAvailable) { 'NotApplicable' }
+ Status = if (-not $mpStatusAvailable) { 'NotEvaluated' }
elseif ($defenderEnabled) { 'InPlace' } else { 'NotInPlace' }
Priority = 'Critical'
Remediation = 'Enable Windows Defender or approved antivirus'
@@ -453,7 +460,7 @@
Description = 'Implement file integrity monitoring'
Expected = 'FIM solution deployed'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'High'
Remediation = 'Implement file integrity monitoring (FIM) solution'
}
@@ -477,7 +484,7 @@
Description = 'Establish security policy'
Expected = 'Security policy documented and reviewed annually'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'High'
Remediation = 'Document and maintain information security policy'
}
@@ -491,7 +498,7 @@
Description = 'Implement incident response plan'
Expected = 'Incident response plan documented and tested'
Actual = 'Manual verification required'
- Status = 'NotApplicable'
+ Status = 'Manual'
Priority = 'Critical'
Remediation = 'Create and test incident response procedures'
}
@@ -507,13 +514,31 @@
$results.Summary.InPlace = @($results.Requirements | Where-Object { $_.Status -eq 'InPlace' }).Count
$results.Summary.NotInPlace = @($results.Requirements | Where-Object { $_.Status -eq 'NotInPlace' }).Count
$results.Summary.NotApplicable = @($results.Requirements | Where-Object { $_.Status -eq 'NotApplicable' }).Count
-
- $testableRequirements = $results.Summary.TotalRequirements - $results.Summary.NotApplicable
- if ($testableRequirements -gt 0) {
+ $results.Summary.Manual = @($results.Requirements | Where-Object { $_.Status -eq 'Manual' }).Count
+ $results.Summary.NotEvaluated = @($results.Requirements | Where-Object { $_.Status -eq 'NotEvaluated' }).Count
+
+ # NotEvaluated stays in the denominator and never counts as compliant;
+ # only NotApplicable and Manual leave the denominator.
+ $scopedRequirements = $results.Summary.TotalRequirements - $results.Summary.NotApplicable - $results.Summary.Manual
+ $evaluatedRequirements = $scopedRequirements - $results.Summary.NotEvaluated
+ $results.Summary.EvaluatedControls = $evaluatedRequirements
+ if ($scopedRequirements -gt 0) {
$results.Summary.CompliancePercentage = [math]::Round(
- ($results.Summary.InPlace / $testableRequirements) * 100, 2
+ ($results.Summary.InPlace / $scopedRequirements) * 100, 2
+ )
+ $results.Summary.CoveragePercentage = [math]::Round(
+ ($evaluatedRequirements / $scopedRequirements) * 100, 2
)
}
+ else {
+ $results.Summary.CompliancePercentage = $null
+ $results.Summary.CoveragePercentage = $null
+ }
+
+ if ($results.Summary.NotEvaluated -gt 0) {
+ $notEvaluatedIds = @($results.Requirements | Where-Object { $_.Status -eq 'NotEvaluated' } | ForEach-Object { $_.RequirementID }) -join ', '
+ Write-Warning "$($results.Summary.NotEvaluated) control(s) could not be evaluated and are counted as not compliant: $notEvaluatedIds"
+ }
# Generate recommendations
$criticalNotInPlace = @($results.Requirements | Where-Object { $_.Status -eq 'NotInPlace' -and $_.Priority -eq 'Critical' })
@@ -527,6 +552,10 @@
if ($highNotInPlace.Count -gt 0) {
$results.Recommendations += "HIGH: $($highNotInPlace.Count) high-priority requirement(s) not in place"
}
+
+ foreach ($notEvaluatedReq in @($results.Requirements | Where-Object { $_.Status -eq 'NotEvaluated' })) {
+ $results.Recommendations += "Could not evaluate $($notEvaluatedReq.RequirementID): run elevated or install the required feature/module"
+ }
if ($results.Summary.CompliancePercentage -lt 100) {
$results.Recommendations += "Full PCI-DSS compliance requires all requirements to be in place"
@@ -543,18 +572,23 @@
}
end {
+ $complianceDisplay = if ($null -eq $results.Summary.CompliancePercentage) { 'N/A' } else { "$($results.Summary.CompliancePercentage)%" }
+ $coverageDisplay = if ($null -eq $results.Summary.CoveragePercentage) { 'N/A' } else { "$($results.Summary.CoveragePercentage)%" }
+
Write-Host "`n=== COMPLIANCE TEST COMPLETE ===" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -eq 100) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 80) { 'Yellow' }
else { 'Red' }
)
-
+
Write-Host "`nCompliance Summary:" -ForegroundColor Cyan
Write-Host " Total Requirements Tested: $($results.Summary.TotalRequirements)" -ForegroundColor White
Write-Host " In Place: $($results.Summary.InPlace)" -ForegroundColor Green
Write-Host " Not In Place: $($results.Summary.NotInPlace)" -ForegroundColor Red
Write-Host " Not Applicable: $($results.Summary.NotApplicable)" -ForegroundColor Gray
- Write-Host " Compliance: $($results.Summary.CompliancePercentage)%" -ForegroundColor $(
+ Write-Host " Manual: $($results.Summary.Manual)" -ForegroundColor Yellow
+ Write-Host " Not Evaluated: $($results.Summary.NotEvaluated)" -ForegroundColor Yellow
+ Write-Host " Compliance: $complianceDisplay (coverage $coverageDisplay, $($results.Summary.NotEvaluated) not evaluated)" -ForegroundColor $(
if ($results.Summary.CompliancePercentage -eq 100) { 'Green' }
elseif ($results.Summary.CompliancePercentage -ge 80) { 'Yellow' }
else { 'Red' }
diff --git a/tests/Compliance.Honesty.Tests.ps1 b/tests/Compliance.Honesty.Tests.ps1
new file mode 100644
index 0000000..0a940c6
--- /dev/null
+++ b/tests/Compliance.Honesty.Tests.ps1
@@ -0,0 +1,172 @@
+#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }
+
+<#
+.SYNOPSIS
+ Compliance-honesty regression tests for the Compliance/ test functions.
+.DESCRIPTION
+ Cross-platform tests (run green on macOS and on the Windows CI lint job).
+
+ Regression-pin for the v1.6.x denominator bug: unevaluable controls used to be
+ emitted as 'NotApplicable' (CIS: 'Manual') and were silently dropped from the
+ compliance-percentage denominator, so e.g. PCI-DSS reported 100% while 8 of 18
+ requirements (incl. Critical 3.5.1/11.3.1) were never evaluated.
+
+ Three distinct states are pinned:
+ * NotEvaluated - the tool tried but could not read the data. Stays in the
+ denominator, never counts as compliant, and triggers a Write-Warning.
+ * Manual - human verification by design. Excluded from the denominator.
+ * NotApplicable- truly not applicable. Excluded (no real occurrences today).
+
+ Both scenarios stub the data-source functions inside the module session state
+ (InModuleScope + function overrides, same pattern as the /tmp/wsa-repro
+ scripts that demonstrated the bug) and pin the exact summary math:
+ scoped = Total - NotApplicable - Manual
+ evaluated = scoped - NotEvaluated (== Summary.EvaluatedControls)
+ CompliancePercentage = compliant / scoped
+ CoveragePercentage = evaluated / scoped
+
+ The module targets Windows PowerShell 5.1+, so only APIs available in
+ 5.1 are used here (no null-coalescing/ternary operators, no ForEach-Object -Parallel).
+
+ Platform guard: the pinned scenarios model a host where the BitLocker, Defender
+ (Get-MpComputerStatus) and secedit data sources are ABSENT - exactly the
+ /tmp/wsa-repro runs. On a real Windows host those cmdlets exist and answer for
+ real, so the pinned counts legitimately differ; both Describe blocks are skipped
+ there (inverse of tests/Smoke.Windows.Tests.ps1).
+#>
+
+BeforeDiscovery {
+ # $IsWindows only exists on PowerShell 6+; on Windows PowerShell 5.1 (which only
+ # ships on Windows) fall back to $env:OS.
+ if ($PSVersionTable.PSVersion.Major -ge 6) {
+ $script:IsWindowsHost = [bool]$IsWindows
+ }
+ else {
+ $script:IsWindowsHost = ($env:OS -eq 'Windows_NT')
+ }
+}
+
+BeforeAll {
+ $script:ModuleRoot = Split-Path -Parent $PSScriptRoot
+ $script:ModuleName = 'WindowsSecurityAudit'
+ $script:ModulePath = Join-Path -Path $script:ModuleRoot -ChildPath "$script:ModuleName.psm1"
+
+ Import-Module -Name $script:ModulePath -Force
+}
+
+AfterAll {
+ Remove-Module -Name $script:ModuleName -Force -ErrorAction SilentlyContinue
+}
+
+Describe 'PCI-DSS compliance honesty (NotEvaluated stays in the denominator)' -Skip:$script:IsWindowsHost {
+ It 'pins the repro: Total=18 InPlace=10 Manual=6 NotEvaluated=2 Compliance=83.33 Coverage=83.33' {
+ InModuleScope -ModuleName $script:ModuleName -ScriptBlock {
+ # Save the module-private helpers the stubs replace; restore in finally.
+ $savedHelpers = @{}
+ foreach ($helperName in @('Get-LocalUserByRid', 'Get-LocalPasswordPolicy', 'Get-AuditPolicyTable')) {
+ $savedHelpers[$helperName] = (Get-Command $helperName).ScriptBlock
+ }
+
+ # Stubs identical to the original repro scenario.
+ function Get-NetFirewallProfile { 'Domain','Private','Public' | ForEach-Object { [pscustomobject]@{Name=$_;Enabled=$true;DefaultInboundAction='Block'} } }
+ function Get-LocalUserByRid { param($Rid) [pscustomobject]@{Enabled=$false} }
+ function Get-LocalPasswordPolicy { [pscustomobject]@{MinPasswordLength=14;MaxPasswordAgeDays=60;LockoutThreshold=5;Source='Api'} }
+ function Get-AuditPolicyTable { param($Subcategory) $t=@{}; foreach ($n in $Subcategory) { $k=((Get-AuditSubcategoryGuid -Name $n) -replace '[{}]','').ToLowerInvariant(); $t[$k]=[pscustomobject]@{Value=3;Success=$true;Failure=$true} }; $t }
+ function Get-WinEvent { [pscustomobject]@{MaximumSizeInBytes=200MB} }
+ function Get-Service { [pscustomobject]@{Name='W32Time';Status='Running'} }
+
+ try {
+ # BitLocker/Get-MpComputerStatus deliberately NOT stubbed: on any host
+ # without those cmdlets (or where they fail) 3.5.1 and 11.3.1 must
+ # surface as NotEvaluated instead of vanishing from the denominator.
+ $r = Test-PciDssCompliance -WarningVariable +pciWarnings 6>$null 3>$null
+ $s = $r.Summary
+
+ $r.Requirements.Count | Should -Be 18
+ $s.InPlace | Should -Be 10
+ $s.NotInPlace | Should -Be 0
+ $s['Manual'] | Should -Be 6
+ $s['NotEvaluated'] | Should -Be 2
+ $s.NotApplicable | Should -Be 0
+ $s.CompliancePercentage | Should -Be 83.33
+ $s['CoveragePercentage'] | Should -Be 83.33
+ $s.EvaluatedControls | Should -Be 10
+
+ $notEvaluatedIds = @($r.Requirements | Where-Object Status -eq 'NotEvaluated' | ForEach-Object RequirementID)
+ $notEvaluatedIds | Should -Be @('3.5.1', '11.3.1')
+
+ # The six human-verification requirements are Manual, not NotApplicable.
+ $manualIds = @($r.Requirements | Where-Object Status -eq 'Manual' | ForEach-Object RequirementID)
+ $manualIds | Should -Be @('3.3.1', '3.6.1', '8.3.1', '11.5.1', '12.1.1', '12.10.1')
+
+ # NotEvaluated requirements must produce the honesty warning and
+ # per-requirement remediation recommendations.
+ @($pciWarnings | Where-Object { "$_" -match '2 control\(s\) could not be evaluated and are counted as not compliant' }).Count |
+ Should -Be 1
+ @($r.Recommendations | Where-Object { $_ -like 'Could not evaluate 3.5.1:*' }).Count | Should -Be 1
+ @($r.Recommendations | Where-Object { $_ -like 'Could not evaluate 11.3.1:*' }).Count | Should -Be 1
+ }
+ finally {
+ foreach ($helperName in $savedHelpers.Keys) {
+ Set-Item -Path "Function:\$helperName" -Value $savedHelpers[$helperName]
+ }
+ Remove-Item -Path Function:\Get-NetFirewallProfile, Function:\Get-WinEvent, Function:\Get-Service -ErrorAction SilentlyContinue
+ }
+ }
+ }
+}
+
+Describe 'CIS Benchmark compliance honesty (Manual bug fix + NotEvaluated)' -Skip:$script:IsWindowsHost {
+ It 'pins the repro: Total=15 Passed=7 Manual=2 NotEvaluated=6 Compliance=53.85 Coverage=53.85' {
+ InModuleScope -ModuleName $script:ModuleName -ScriptBlock {
+ # Save the module-private helpers the stubs replace; restore in finally.
+ $savedHelpers = @{}
+ foreach ($helperName in @('Get-LocalUserByRid', 'Get-AuditPolicyTable')) {
+ $savedHelpers[$helperName] = (Get-Command $helperName).ScriptBlock
+ }
+
+ # Stubs identical to the original repro scenario: no firewall profiles,
+ # secedit export produces nothing -> CIS-1.1.1..5 and CIS-9.1.1 unevaluable.
+ function Get-NetFirewallProfile { }
+ function secedit { }
+ function Get-LocalUserByRid { param($Rid) [pscustomobject]@{Enabled=$false} }
+ function Get-AuditPolicyTable { param($Subcategory) $t=@{}; foreach ($n in $Subcategory) { $k=((Get-AuditSubcategoryGuid -Name $n) -replace '[{}]','').ToLowerInvariant(); $t[$k]=[pscustomobject]@{Value=3;Success=$true;Failure=$true} }; $t }
+ function Test-Path { param($Path) $Path -like 'HKLM:*' }
+ function Get-ItemProperty { [pscustomobject]@{DontDisplayLastUserName=1;NoLMHash=1} }
+
+ try {
+ $r = Test-CISBenchmark -Level 1 -WarningVariable +cisWarnings 6>$null 3>$null
+ $s = $r.Summary
+
+ $r.Controls.Count | Should -Be 15
+ $s.Passed | Should -Be 7
+ $s.Failed | Should -Be 0
+ $s['Manual'] | Should -Be 2
+ $s['NotEvaluated'] | Should -Be 6
+ # Regression: NotApplicable must count only real NotApplicable controls
+ # (it used to store the Manual count, which would be 8 here pre-remap).
+ $s.NotApplicable | Should -Be 0
+ $s.CompliancePercentage | Should -Be 53.85
+ $s['CoveragePercentage'] | Should -Be 53.85
+ $s.EvaluatedControls | Should -Be 7
+
+ $notEvaluatedIds = @($r.Controls | Where-Object Status -eq 'NotEvaluated' | ForEach-Object ControlID)
+ $notEvaluatedIds | Should -Be @('CIS-1.1.1', 'CIS-1.1.2', 'CIS-1.1.3', 'CIS-1.1.4', 'CIS-1.1.5', 'CIS-9.1.1')
+
+ # CIS-2.2.1 / CIS-2.2.3 remain Manual by design.
+ $manualIds = @($r.Controls | Where-Object Status -eq 'Manual' | ForEach-Object ControlID)
+ $manualIds | Should -Be @('CIS-2.2.1', 'CIS-2.2.3')
+
+ @($cisWarnings | Where-Object { "$_" -match '6 control\(s\) could not be evaluated and are counted as not compliant' }).Count |
+ Should -Be 1
+ @($r.Recommendations | Where-Object { $_ -like 'Could not evaluate CIS-9.1.1:*' }).Count | Should -Be 1
+ }
+ finally {
+ foreach ($helperName in $savedHelpers.Keys) {
+ Set-Item -Path "Function:\$helperName" -Value $savedHelpers[$helperName]
+ }
+ Remove-Item -Path Function:\Get-NetFirewallProfile, Function:\secedit, Function:\Test-Path, Function:\Get-ItemProperty -ErrorAction SilentlyContinue
+ }
+ }
+ }
+}