Pin audit sink writes against ambient WhatIfPreference

Add-Content/New-Item in Write-PersonaAuditRecord honour ShouldProcess, so
an ambient $WhatIfPreference left set in the caller's session (e.g. from
dot-sourcing a prior -WhatIf run) silently turned the audit write into a
no-op, even though the sink itself never opts into ShouldProcess. The
audit log is supposed to be unconditional under -WhatIf, so both calls
now pin -WhatIf:$false -Confirm:$false.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-24 10:16:37 -04:00
parent a54ff3c8f2
commit 8c2dc842e6
+10 -2
View File
@@ -69,12 +69,20 @@ function Write-PersonaAuditRecord {
$directory = Split-Path -Parent $Path
if ($directory -and -not (Test-Path -LiteralPath $directory)) {
$null = New-Item -ItemType Directory -Path $directory -Force
$null = New-Item -ItemType Directory -Path $directory -Force -WhatIf:$false -Confirm:$false
}
# Append, one record per line. UTF-8 without BOM so the file is
# machine-readable by any NDJSON consumer.
Add-Content -LiteralPath $Path -Value $line -Encoding utf8NoBOM -ErrorAction Stop
#
# -WhatIf:$false / -Confirm:$false pin this call regardless of any
# ambient $WhatIfPreference in the caller's session (e.g. left set by
# dot-sourcing an earlier -WhatIf run). Add-Content honours
# ShouldProcess, and this sink is not optional under -WhatIf - the
# audit trail is what makes preview mode auditable at all, so it must
# write unconditionally, independent of anything the caller's scope
# happens to have set.
Add-Content -LiteralPath $Path -Value $line -Encoding utf8NoBOM -ErrorAction Stop -WhatIf:$false -Confirm:$false
}
catch {
if ($null -ne $State -and $State.FileSinkFailed) { return }