Add CompanyName and Department to the results CSV

Add-PersonaRunResult now optionally accepts the normalized UserRecord and
pulls CompanyName/Department from it for the results.csv row, via
TryGetValue rather than the Properties dictionary indexer so a record built
without those keys doesn't throw. Kept off the PersonaDecisionResult/audit
contract on purpose - this is CSV-only, not a widening of what gets logged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-25 00:25:36 -04:00
parent 5f125c34f2
commit d1b0ac992e
6 changed files with 30 additions and 9 deletions
+1 -1
View File
@@ -196,7 +196,7 @@ function Invoke-PersonaEngineRun {
Write-UserPersonaResult -Result $result
Add-PersonaRunResult -Counters $counters -Result $result
Add-PersonaRunResult -Counters $counters -Result $result -UserRecord $record
New-PersonaAuditRecord -Context $Context -RecordType 'UserEvent' -Result $result `
-PreviousValue $previousValue -IncludeTrace:$Tracing |
+21 -1
View File
@@ -96,11 +96,18 @@ function Add-PersonaRunResult {
.PARAMETER Result
A PersonaDecisionResult with both Outcome and Action populated.
.PARAMETER UserRecord
The normalized UserRecord the decision was made from. Optional, and used
only to carry CompanyName/Department onto the results CSV row - it is never
written to counters or audit records, so this does not widen what a
PersonaDecisionResult itself carries.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)] [object] $Counters,
[Parameter(Mandatory)] [object] $Result
[Parameter(Mandatory)] [object] $Result,
[object] $UserRecord
)
$Counters.Processed++
@@ -124,11 +131,24 @@ function Add-PersonaRunResult {
'Skipped' { $Counters.Skipped++ }
}
# TryGetValue rather than the indexer: Properties is a case-insensitive
# Dictionary, whose indexer throws on a missing key rather than returning
# $null. CompanyName/Department are always present when UserRecord comes from
# ConvertTo-PersonaUserRecord, but this stays safe for any other caller too.
$companyName = $null
$department = $null
if ($UserRecord) {
$null = $UserRecord.Properties.TryGetValue('CompanyName', [ref]$companyName)
$null = $UserRecord.Properties.TryGetValue('Department', [ref]$department)
}
# Matched carries the assigned persona; Unclassified/EvaluationError carry
# their outcome name, since CalculatedPersona is 'Unclassified' or $null there.
$Counters.Results.Add([pscustomobject]@{
AccountObjectId = [string]$Result.AccountObjectId
UserPrincipalName = [string]$Result.UserPrincipalName
PersonaStatus = [string]$Result.Outcome -eq 'Matched' ? [string]$Result.CalculatedPersona : [string]$Result.Outcome
CompanyName = [string]$companyName
Department = [string]$department
})
}