PoshCode Archive  Artifact [8d582c246f]

Artifact 8d582c246fb1fc5d779ea09022ccca8c2140988807f616f872a34d29cff1bcb0:

  • File ConvertTo-CliXml.ps1 — part of check-in [c43d642d7a] at 2018-06-10 13:08:23 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 (user: David Sjstrand size: 1556)

# 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
# version: 0.1
# type: function
# author: David Sjstrand
# license: CC0
# function: ConvertTo-CliXml
# x-poshcode-id: 2301
# x-derived-from-id: 4050
# x-archived: 2016-06-09T05:22:34
# x-published: 2011-10-14T10:50:00
#
#
#requires -version 2.0
function ConvertTo-CliXml {
    param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [PSObject[]]$InputObject
    )
    begin {
        $type = [type]::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()
    }
}