When Remove-OrphanedSid changes an ACL, the log is how you find out later what it did and why. That’s why every command takes -CorrelationId. The first post ran its removal with -CorrelationId 'CHG0012345', so you could search for the change ticket number and pull every line of that run out of the log, and every row of its CSV.
Through 1.0.1 that worked for most lines. Eight issues between 1.0.1 and 1.1.0 covered the ways the logging fell short of what the help promised. Some of the gaps were small. Others meant one run’s log ended up in two files, or tonight’s scheduled run wiped out last night’s log.
I ran each case again on the lab DC this week, against the 1.0.1 and 1.1.0 packages, each in a fresh process. Every run but one was a read-only Find-OrphanedSid against the test OU. The one that wasn’t, a removal from a test contact, is in the last section.
I had the log built specifically for auditing, piping to a SIEM to hold off questions from my InfoSec buddies. What changed? RTFL (read the funky logs).
A change number on every line
The help said -CorrelationId appears on every log line and result row. I passed -CorrelationId 'DEMO-42' on both versions:
| Module | Log entries | Entries with another ID | Result rows with DEMO-42 |
|---|---|---|---|
| 1.0.1 | 36 | 10, carrying 3 different IDs | 0 of 2 |
| 1.1.0 | 44 | 0 | 2 of 2 |
Every result row on 1.0.1 had its own GUID. So did every CSV row. Filter the CSV on your change number and you’d get nothing back.
The cause was nine calls between module functions that didn’t pass the ID along. Each function on the other end had a default, and the default was a new GUID. Discovery was one of them:
| |
Detection was another:
| |
The nine were found by parsing every call from one module function to another, and flagging each call to a function that takes -CorrelationId where the call didn’t pass it. One hit was left alone: a call inside a [ValidateScript()] attribute, which runs before the parameter it would pass is bound.
The test that keeps it fixed reads the log the way you would. It runs Find-OrphanedSid and Remove-OrphanedSid with a fixed ID and pulls the ID from every text line and every JSON line. Any line or result row that carries another one fails it:
| |
Two files for one run
Issue #90 was a config file with LogFilePath in it. Give the run both that and a -LogPath, and on 1.0.1 your log came out like this:
| File | Lines on 1.0.1 | Lines on 1.1.0 |
|---|---|---|
-LogPath file | 14 | 48 |
Config LogFilePath | 26 | not created |
The 14 lines in the -LogPath file were the header and set-up, ending with Memory manager initialized successfully. Everything after the configuration loaded went to the other file. The setup chose a log, wrote to it, then chose again:
The elseif branch caused a split of its own. Initialize-LogDirectory had already resolved -LogPath into a full path, and this copied the raw value back over it. Issue #45 was about where a relative path went. Resolve-LogPath joined it to the module’s folder, so -LogPath .\run.log pointed into the install folder, under Program Files if you’d installed the module. When I ran it on 1.0.1, both bugs showed up at once:
The header went into the module folder. The copy-back then put the bare rel.log back, and Add-Content resolves a relative path against the current location, so the rest of the run went there.
1.1.0 picks the log file once, before anything is written, and what you type on the command line wins:
| |
The configured path then goes through the same resolution, refusal and folder creation as -LogPath. A relative path resolves against the current location with GetUnresolvedProviderPathFromPSPath, the same call the last post used for a relative -BackupPath. The copy-back is gone.
A blank path that failed its own check
Pass -LogPath ' ' and you should’ve gotten the default name. Resolve-LogPath had code for that, and it didn’t run:
A validation attribute on a parameter stays on the variable. Assigning to $LogPath later in the function runs the check again, and '' fails it. Same result on 5.1 and 7:
| |
The fix trims into a separate variable. That was issue #31, one of the bugs the test cleanup filed.
The nightly log that kept one night
If you run it from a scheduled task, you probably reuse one -LogPath, something like nightly.log. On 1.0.1, every run started by recreating the file:
I ran it twice into one file on the DC:
| Module | After run 1 | After run 2 | Runs in the file |
|---|---|---|---|
| 1.0.1 | 9,610 bytes | 9,610 bytes | 1 |
| 1.1.0 | 12,749 bytes | 25,497 bytes | 2 |
When #44 was filed, a 12 MB log went to 1.3 KB. The rotation settings, EnableLogFileRotation and MaxLogFileSizeMB, checked the size after the file had been emptied, so they didn’t apply to -LogPath at all.
1.1.0 appends the header to an existing file after a blank line. Rotation happens before the run writes anything, so one run’s log stays in one file:
| |
On the DC, I grew a log to 1.54 MB with MaxLogFileSizeMB set to 1. The next run renamed it to nightly_<timestamp>.log, holding the two earlier runs. The new file started with Rotated log file to: ... and held only the third. Rotated files pile up until you prune them, and the troubleshooting guide shows how.
Settings the loader read and ignored
The config loader validated a LogLevel key and read an EnableDetailedLogging key. Nothing applied either one. I tried a config file of just {"LogLevel": "Error"}:
| Module | Log lines | [Information] lines |
|---|---|---|
| 1.0.1 | 40 | 22 |
| 1.1.0 | 20 | 0 |
The 20 lines left on 1.1.0 are the 12-line header and 8 security audit entries, which bypass the level filter. The level now follows the same order as the path: -LogLevel, then -Verbose or -Debug, then the file, then Information. EnableDetailedLogging is gone. If your config file still sets it, it loads without complaint, because the loader only reads the keys it knows.
Audit entries that said less than they claimed
The security audit lines are JSON, and Write-SecurityLog builds a message for each one with the event type and outcome in front:
| |
The first audit line of a run on each version:
The entry’s data also carried the plain message under the key Message. Format-LogMessage copies data keys onto the entry with Add-Member -Force, and -Force replaces a property that’s already there. The plain text won. The unit tests mocked the writer, so they didn’t see the overwrite. An integration test now reads every SecurityAudit line a removal writes and checks that each message starts with its own [type] [outcome].
The removal’s own audit entry was worse:
| |
The removal result didn’t have BackupCreated, BackupPath or RemovedCount, so all three were empty on every entry. The entry was also Success whatever happened, because Remove-OrphanedSidEntry reports a failure in its result and doesn’t throw, and no caller read the result. The unit test passed because its stand-in for Remove-OrphanedSidEntry returned the three missing properties itself.
In 1.1.0, New-ACLBackup returns the path of the file it wrote, and the result carries it as BackupFile. The audit entry records what happened:
The entry is a Failure, with the error message, when the removal failed. BackupFile is on the Remove-OrphanedSid output too, so the result tells you which file to restore from. On the DC, I ran a removal against a test contact. Its BackupFile pointed into the run’s backup folder, and its audit line carried the same path with ActualRemovals = 1.
