PoshCode Archive  Artifact [44e3025ef5]

Artifact 44e3025ef595104f6770f0a0cf90ac5c842ea7d10597a5430377968c06f93cb7:

  • File ConvertTo-CliXml.ps1 — part of check-in [e22722223b] at 2018-06-10 12:59:52 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: unknown size: 1370)

# 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
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 1672
# x-archived: 2010-03-30T09:01:13
#
#
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 {
            $method.invoke($serializer, $object)
        } catch {
            write-warning "Could not serialize $($object.gettype()): $_"
        }
    }
    end {    
        $done.invoke($serializer, @())
        $sw.ToString()
    }
}