ConvertTo-CliXml (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1672"></script>download | new post
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).
- 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()
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository