CliXml.ps1
#requires -version 2.0 function ConvertFrom-CliXml { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [ValidateNotNullOrEmpty()] [String[]]$InputObject ) begin { $OFS = "`n" [String]$xmlString = "" } process { $xmlString += $InputObject } end { $type = [PSObject].Assembly.GetType('System.Management.Automation.Deserializer') $ctor = $type.GetConstructor('instance,nonpublic', $null, @([xml.xmlreader]), $null) $sr = New-Object System.IO.StringReader $xmlString $xr = New-Object System.Xml.XmlTextReader $sr $deserializer = $ctor.Invoke($xr) $done = $type.GetMethod('Done', [System.Reflection.BindingFlags]'nonpublic,instance') while (!$type.InvokeMember("Done", "InvokeMethod,NonPublic,Instance", $null, $deserializer, @())) { try { $type.InvokeMember("Deserialize", "InvokeMethod,NonPublic,Instance", $null, $deserializer, @()) } catch { Write-Warning "Could not deserialize ${string}: $_" } } $xr.Close() $sr.Dispose() } } function ConvertTo-CliXml { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [ValidateNotNullOrEmpty()] [PSObject[]]$InputObject ) begin { $type = [PSObject].Assembly.GetType('System.Management.Automation.Serializer') $ctor = $type.GetConstructor('instance,nonpublic', $null, @([System.Xml.XmlWriter]), $null) $sw = New-Object System.IO.StringWriter $xw = New-Object System.Xml.XmlTextWriter $sw $serializer = $ctor.Invoke($xw) #$method = $type.GetMethod('Serialize', 'nonpublic,instance', $null, [type[]]@([object]), $null) } process { try { [void]$type.InvokeMember("Serialize", "InvokeMethod,NonPublic,Instance", $null, $serializer, [object[]]@($InputObject)) } catch { Write-Warning "Could not serialize $($InputObject.GetType()): $_" } } end { [void]$type.InvokeMember("Done", "InvokeMethod,NonPublic,Instance", $null, $serializer, @()) $sw.ToString() $xw.Close() $sw.Dispose() } } $filesIn = ls | select * -first 2 $xml = $filesIn | ConvertTo-CliXml $filesOut = $xml | ConvertFrom-CliXml Compare-Object $filesIn $filesOut -IncludeEqual |