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 #4,#7: WhatIf writes nothing to disk (rollback content returned in RollbackPreview); fake SFC 'repairing' block replaced with honest RepairGuidance, RepairAttempted set only on real DISM RestoreHealth

Okan YILDIZ5a590c59acf2
diff --git a/Core/Test-SystemIntegrity.ps1 b/Core/Test-SystemIntegrity.ps1
index fd4f67c..20a88d6 100644
--- a/Core/Test-SystemIntegrity.ps1
+++ b/Core/Test-SystemIntegrity.ps1
@@ -40,6 +40,7 @@ function Test-SystemIntegrity {
CorruptedFiles = @()
RepairAttempted = $false
RepairSuccessful = $false
+ RepairGuidance = $null
WindowsUpdateServices = @() # Added this property
}
}
@@ -89,8 +90,11 @@ function Test-SystemIntegrity {
$results.CorruptedFiles = $cannotRepairLines
if ($RepairFiles) {
- Write-Host "Attempting to repair corrupt files..." -ForegroundColor Yellow
- $results.RepairAttempted = $true
+ # SFC already repaired everything it could during /scannow; the
+ # remaining 'Cannot repair' files need a DISM RestoreHealth pass
+ # first. Report honest guidance instead of faking a repair attempt.
+ $results.RepairGuidance = 'SFC already repairs what it can during /scannow. For the files listed in CorruptedFiles run: DISM /Online /Cleanup-Image /RestoreHealth, then sfc /scannow again.'
+ Write-Warning $results.RepairGuidance
}
}
elseif ($cbsLogAvailable -and $cbsTimestampsParseable) {
@@ -150,6 +154,7 @@ function Test-SystemIntegrity {
Write-Host "Attempting DISM repair..." -ForegroundColor Yellow
$null = & dism /Online /Cleanup-Image /RestoreHealth 2>&1 | Out-String
$dismRepairExitCode = $LASTEXITCODE
+ $results.RepairAttempted = $true
if ($dismRepairExitCode -eq 0) {
$results.RepairSuccessful = $true
diff --git a/Hardening/Invoke-SecurityRemediation.ps1 b/Hardening/Invoke-SecurityRemediation.ps1
index 055da59..7ef7798 100644
--- a/Hardening/Invoke-SecurityRemediation.ps1
+++ b/Hardening/Invoke-SecurityRemediation.ps1
@@ -28,7 +28,9 @@ function Invoke-SecurityRemediation {
Invoke-SecurityAssessment -OutputPath "C:\Assessments" | Invoke-SecurityRemediation -GenerateRollback -Confirm
Pipe an assessment object directly into remediation with confirmation prompts
.OUTPUTS
- PSCustomObject with FindingsProcessed, Remediated, ManualReview, Failed, and RollbackPath
+ PSCustomObject with FindingsProcessed, Remediated, ManualReview, Failed, RollbackPath,
+ and RollbackPreview (under -WhatIf the rollback script content is returned in
+ RollbackPreview instead of being written to disk)
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'File')]
@@ -96,6 +98,7 @@ function Invoke-SecurityRemediation {
ManualReview = @()
Failed = @()
RollbackPath = $null
+ RollbackPreview = $null
}
$rollbackScriptLines = @()
@@ -500,28 +503,24 @@ function Invoke-SecurityRemediation {
}
$header += ""
- # The rollback script is an output artifact (like a report),
- # so it is written even under -WhatIf - that is the documented
- # 'preview fixes with a rollback script, no changes' behavior.
- # ShouldProcess still gates the write on real (non-WhatIf) runs.
- if ($WhatIfPreference -or $PSCmdlet.ShouldProcess($rollbackFile, "Write rollback script")) {
- # -WhatIf:$false: New-Item/Set-Content support ShouldProcess and
- # would inherit $WhatIfPreference, silently skipping the write -
- # the rollback script is an output artifact and must really be written
+ $rollbackContent = ($header + $rollbackScriptLines) -join [Environment]::NewLine
+
+ if ($WhatIfPreference) {
+ # WhatIf contract: NEVER write to disk. The full rollback
+ # script content is returned on the result object instead.
+ $results.RollbackPreview = $rollbackContent
+ Write-Host "`n[ROLLBACK] WhatIf: would write rollback script to $rollbackFile (content returned in RollbackPreview)" -ForegroundColor Magenta
+ }
+ elseif ($PSCmdlet.ShouldProcess($rollbackFile, "Write rollback script")) {
if (-not (Test-Path -Path $RollbackDirectory)) {
- New-Item -Path $RollbackDirectory -ItemType Directory -Force -WhatIf:$false | Out-Null
+ New-Item -Path $RollbackDirectory -ItemType Directory -Force | Out-Null
}
- Set-Content -Path $rollbackFile -Value (($header + $rollbackScriptLines) -join [Environment]::NewLine) -Encoding UTF8 -ErrorAction Stop -WhatIf:$false
+ Set-Content -Path $rollbackFile -Value $rollbackContent -Encoding UTF8 -ErrorAction Stop
$results.RollbackPath = $rollbackFile
- if ($WhatIfPreference) {
- Write-Host "`n[ROLLBACK] WhatIf preview rollback script written: $rollbackFile" -ForegroundColor Magenta
- }
- else {
- Write-Host "`n[ROLLBACK] Rollback script written: $rollbackFile" -ForegroundColor Green
- }
+ Write-Host "`n[ROLLBACK] Rollback script written: $rollbackFile" -ForegroundColor Green
}
else {
- Write-Host "`n[ROLLBACK] WhatIf: would write rollback script to $rollbackFile" -ForegroundColor Magenta
+ Write-Host "`n[ROLLBACK] Rollback script write declined (ShouldProcess)" -ForegroundColor Yellow
}
}
catch {
diff --git a/tests/Smoke.Windows.Tests.ps1 b/tests/Smoke.Windows.Tests.ps1
index 0206382..7a7a650 100644
--- a/tests/Smoke.Windows.Tests.ps1
+++ b/tests/Smoke.Windows.Tests.ps1
@@ -356,14 +356,15 @@ Describe 'WindowsSecurityAudit real-Windows smoke tests' -Tag @('Smoke', 'Window
}
$result = Invoke-SecurityRemediation -AssessmentPath $assessmentJson -GenerateRollback -WhatIf -RollbackDirectory $TestDrive
- Assert-ResultShape -Object $result -Names @('RemediationDate', 'MaxSeverity', 'WhatIf', 'FindingsProcessed', 'Remediated', 'ManualReview', 'Failed', 'RollbackPath')
+ Assert-ResultShape -Object $result -Names @('RemediationDate', 'MaxSeverity', 'WhatIf', 'FindingsProcessed', 'Remediated', 'ManualReview', 'Failed', 'RollbackPath', 'RollbackPreview')
$result.WhatIf | Should -BeTrue
- # WhatIf: nothing may be applied; since v1.6.1 a rollback PREVIEW script is
- # generated (README contract: preview fixes with a rollback script, no changes)
+ # Madde-4 WhatIf contract: nothing may be applied and NOTHING may be written
+ # to disk - no rollback file under TestDrive, RollbackPath stays null, and the
+ # would-be script content is returned in RollbackPreview instead
@($result.Remediated).Count | Should -Be 0
- $result.RollbackPath | Should -Not -BeNullOrEmpty
- $result.RollbackPath | Should -Exist
- (Get-Content -Path $result.RollbackPath -Raw) | Should -Match 'WHATIF PREVIEW'
+ $result.RollbackPath | Should -BeNullOrEmpty
+ @(Get-ChildItem -Path $TestDrive -Filter 'SecurityRemediation_Rollback_*.ps1' -Recurse -ErrorAction SilentlyContinue).Count | Should -Be 0
+ $result.RollbackPreview | Should -Match 'WHATIF PREVIEW'
}
It 'Update-DefenderConfiguration honors -WhatIf without changing the system' {