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:
@@ -69,12 +69,20 @@ function Write-PersonaAuditRecord {
|
|||||||
|
|
||||||
$directory = Split-Path -Parent $Path
|
$directory = Split-Path -Parent $Path
|
||||||
if ($directory -and -not (Test-Path -LiteralPath $directory)) {
|
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
|
# Append, one record per line. UTF-8 without BOM so the file is
|
||||||
# machine-readable by any NDJSON consumer.
|
# 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 {
|
catch {
|
||||||
if ($null -ne $State -and $State.FileSinkFailed) { return }
|
if ($null -ne $State -and $State.FileSinkFailed) { return }
|
||||||
|
|||||||
Reference in New Issue
Block a user