ConvertClixml.psm1

<#
    .SYNOPSIS
        ConvertClixmls Clixml to an object.
 
    .DESCRIPTION
        ConvertClixmls Clixml to an object.
 
    .PARAMETER String
        Clixml as a string object.
 
    .EXAMPLE
        $xml = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>ThisIsMyString</S>
</Objs>
"@
        ConvertFrom-Clixml -String $xml
 
        ThisIsMyString
 
    .EXAMPLE
        $xml = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>ThisIsMyString</S>
</Objs>
"@
        $xml | ConvertFrom-Clixml
 
        ThisIsMyString
 
    .EXAMPLE
        $xml = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>ThisIsMyString</S>
</Objs>
"@
        $xml2 = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>This is another string</S>
</Objs>
"@
        ConvertFrom-Clixml -String $xml,$xml2
 
        ThisIsMyString
        This is another string
 
    .EXAMPLE
        $xml = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>ThisIsMyString</S>
</Objs>
"@
        $xml2 = @"
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<S>This is another string</S>
</Objs>
"@
        $xml,$xml2 | ConvertFrom-Clixml
 
        ThisIsMyString
        This is another string
 
    .OUTPUTS
        [Object[]]
 
    .LINK
        http://ConvertClixml.readthedocs.io/en/latest/functions/ConvertFrom-Clixml/
#>

function ConvertFrom-Clixml {
    [CmdletBinding(HelpUri = 'http://ConvertClixml.readthedocs.io/en/latest/functions/ConvertFrom-Clixml/')]
    param
    (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $String
    )

    begin {
        $userErrorActionPreference = $ErrorActionPreference
        Set-Variable -Name 'SPLIT' -Value '(?<!^)(?=<Objs)' -Option 'Constant'
    }

    process {
        foreach ($s in $String) {
            try {
                foreach ($record in ($s -split $SPLIT)) {
                    [System.Management.Automation.PSSerializer]::Deserialize($record)
                }
            } catch {
                Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference
            }
        }
    }
}


<#
    .SYNOPSIS
        ConvertClixmls an object to Clixml.
 
    .DESCRIPTION
        ConvertClixmls an object to Clixml.
 
    .PARAMETER InputObject
        The input object to serialize
 
    .PARAMETER Depth
        The depth of the members to serialize
 
    .EXAMPLE
        $string = 'A string'
        ConvertTo-Clixml -InputObject $string
 
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>A string</S>
</Objs>
 
    .EXAMPLE
        $string = 'A string'
        $string | ConvertTo-Clixml
 
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>A string</S>
</Objs>
 
    .EXAMPLE
        $string1 = 'A string'
        $string2 = 'Another string'
        ConvertTo-Clixml -InputObject $string1,$string2
 
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>A string</S>
</Objs>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>Another string</S>
</Objs>
 
    .EXAMPLE
        $string1 = 'A string'
        $string2 = 'Another string'
        $string1,$string2 | ConvertTo-Clixml
 
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>A string</S>
</Objs>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <S>Another string</S>
</Objs>
 
    .OUTPUTS
        [String[]]
 
    .LINK
        http://ConvertClixml.readthedocs.io/en/latest/functions/ConvertTo-Clixml/
#>

function ConvertTo-Clixml {
    [CmdletBinding(HelpUri = 'http://ConvertClixml.readthedocs.io/en/latest/functions/ConvertTo-Clixml/')]
    param
    (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [PSObject]
        $InputObject,

        [Parameter(Mandatory = $false)]
        [ValidateRange(1, [Int32]::MaxValue)]
        [Int32]
        $Depth = 1
    )

    begin {
        $userErrorActionPreference = $ErrorActionPreference
    }

    process {
        foreach ($io in $InputObject) {
            try {
                [System.Management.Automation.PSSerializer]::Serialize($io, $Depth)
            } catch {
                Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference
            }
        }
    }
}