PoshCode Archive  Artifact [07937666cd]

Artifact 07937666cd914dee8dc550cb7d3a49f029403f539d0b5bb2a821fbdd7eaecd83:

  • File ConvertTo-CliXml.ps1 — part of check-in [59e1affc90] at 2018-06-10 13:08:14 on branch trunk — Export-CliXml and Import-CliXml only work with files. This is an implementation for sending CliXML directly to the pipeline. Probably needs v2 powershell (not tested in v1). (user: Oisin Grehan size: 1469)

# 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. Probably needs v2 powershell (not tested in v1).
# version: 0.1
# type: function
# author: Oisin Grehan
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 2293
# x-derived-from-id: 2301
# x-archived: 2014-01-03T12:10:56
# x-published: 2011-10-10T14:40:00
#
#
function ConvertTo-CliXml {
    param(
        [parameter(position=0,mandatory=$true,valuefrompipeline=$true)]
        [validatenotnull()]
        [psobject]$object
    )
    begin {
        $type = [type]::gettype("System.Management.Automation.Serializer")
        $ctor = $type.getconstructor("instance,nonpublic", $null, @([xml.xmlwriter]), $null)
        $sw = new-object io.stringwriter
        $xw = new-object xml.xmltextwriter $sw
        $serializer = $ctor.invoke($xw)
        $method = $type.getmethod("Serialize", "nonpublic,instance", $null, [type[]]@([object]), $null)
        $done = $type.getmethod("Done", [reflection.bindingflags]"nonpublic,instance")
    }
    process {
        try {
            [void]$method.invoke($serializer, $object)
        } catch {
            write-warning "Could not serialize $($object.gettype()): $_"
        }
    }
    end {    
        [void]$done.invoke($serializer, @())
        $sw.ToString()
    }
}