# encoding: ascii
# api: powershell
# title: ConvertTo-CliXml
# description: Export-CliXml and Import-CliXml only work with files. This is an implementation for sending CliXML directly to the pipeline. Requires PowerShell 2.0 or later.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 4050
# x-derived-from-id: 4544
# x-archived: 2016-03-20T13:29:54
# x-published: 2016-03-27T14:36:00
#
#
#requires -version 2.0
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)
$done = $type.GetMethod('Done', [System.Reflection.BindingFlags]'nonpublic,instance')
}
process {
try {
[void]$method.Invoke($serializer, $InputObject)
} catch {
Write-Warning "Could not serialize $($InputObject.GetType()): $_"
}
}
end {
[void]$done.Invoke($serializer, @())
$sw.ToString()
$xw.Close()
$sw.Dispose()
}
}