PoshCode Archive  Artifact [c0c3f27ae3]

Artifact c0c3f27ae3d772d0f1dc5c24844667efdb803eabdfbeca6694bff8b0bc7105a7:

  • File ConvertTo-CliXml.ps1 — part of check-in [f20a565c31] at 2018-06-10 13:35:28 on branch trunk — 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. (user: Joel Bennett size: 1588)

# 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()
    }
}