Avoid decorating ErrorRecord during CliXml serialization by KirtiRamchandani · Pull Request #27528 · PowerShell/PowerShell
PR Summary
Fixes #25854.
ConvertTo-CliXml and Export-CliXml serialize ErrorRecord instances by adding remoting-specific note properties such as ErrorCategory_Activity and InvocationInfo. The serializer was adding those properties to the caller's PSObject wrapper, which caused the original ErrorRecord to gain serialization-only extended members after serialization completed.
This change serializes remoting records through a wrapper with local instance members, preserving the generated CliXml fields without decorating the caller's input object.
PR Context
PSObject.Copy() alone still shares the base object's resurrection-table key, so note properties added to the copy are visible on wrappers around the same base object. The new helper resets the serialization wrapper's instance-member collection before adding remoting-only properties, while preserving existing visible instance members for serialization.
The same path is used for informational records, so the isolation is applied there too.
PR Checklist
- Tests pass
Tests
- Reproduced before fix: serializing an
ErrorRecordincreased extended members from 1 to 13 and addedErrorCategory_*,InvocationInfo,SerializeExtendedInfo, and related serialization notes. - Added regression coverage for both
Export-ClixmlandConvertTo-CliXml. Start-PSBuild -Configuration Debug -SMAOnly- Manual verification after fix:
ConvertTo-CliXmlandExport-Clixmladded no extended members to the originalErrorRecord. - Manual verification after fix: generated CliXml still contains
ErrorCategory_CategoryandConvertFrom-CliXmlround-trips the error information. Start-PSPester -Path .\test\powershell\Modules\Microsoft.PowerShell.Utility\clixml.tests.ps1 -Terse -ThrowOnFailure -SkipTestToolBuild -BinDir <publish>(19 passed)git diff --check(line-ending warning only)