# 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+ (now works in 3.0 and 4.0)
# version: 0.1
# type: function
# author: Poshoholic
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 4544
# x-archived: 2016-03-05T18:20:13
# x-published: 2016-10-23T01:13: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)
}
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()
}
}