Public/ConvertFrom-MemoryStream.ps1

<#
    .SYNOPSIS
        Converts MemoryStream to a base64 encoded string.
 
    .DESCRIPTION
        Converts MemoryStream to a base64 encoded string.
 
    .PARAMETER MemoryStream
        A MemoryStream object for conversion.
 
    .PARAMETER Encoding
        The encoding to use for conversion.
        Defaults to UTF8.
        Valid options are ASCII, BigEndianUnicode, Default, Unicode, UTF32, UTF7, and UTF8.
 
    .PARAMETER ToString
        Switch parameter to specify a conversion to a string object.
 
    .PARAMETER ToBase64
        Switch parameter to specify a conversion to a Base64 encoded string object.
 
    .EXAMPLE
        $string = 'A string'
        $stream = [System.IO.MemoryStream]::new()
        $writer = [System.IO.StreamWriter]::new($stream)
        $writer.Write($string)
        $writer.Flush()
 
        ConvertFrom-MemoryStream -MemoryStream $stream -ToBase64
 
        QSBzdHJpbmc=
 
    .EXAMPLE
        $string = 'A string'
        $stream = [System.IO.MemoryStream]::new()
        $writer = [System.IO.StreamWriter]::new($stream)
        $writer.Write($string)
        $writer.Flush()
 
        $stream | ConvertFrom-MemoryStream -ToBase64
 
        QSBzdHJpbmc=
 
    .EXAMPLE
        $string1 = 'A string'
        $stream1 = [System.IO.MemoryStream]::new()
        $writer1 = [System.IO.StreamWriter]::new($stream1)
        $writer1.Write($string1)
        $writer1.Flush()
 
        $string2 = 'Another string'
        $stream2 = [System.IO.MemoryStream]::new()
        $writer2 = [System.IO.StreamWriter]::new($stream2)
        $writer2.Write($string2)
        $writer2.Flush()
 
        ConvertFrom-MemoryStream -MemoryStream $stream1,$stream2 -ToBase64
 
        QSBzdHJpbmc=
        QW5vdGhlciBzdHJpbmc=
 
    .EXAMPLE
        $string1 = 'A string'
        $stream1 = [System.IO.MemoryStream]::new()
        $writer1 = [System.IO.StreamWriter]::new($stream1)
        $writer1.Write($string1)
        $writer1.Flush()
 
        $string2 = 'Another string'
        $stream2 = [System.IO.MemoryStream]::new()
        $writer2 = [System.IO.StreamWriter]::new($stream2)
        $writer2.Write($string2)
        $writer2.Flush()
 
        $stream1,$stream2 | ConvertFrom-MemoryStream -ToBase64
 
        QSBzdHJpbmc=
        QW5vdGhlciBzdHJpbmc=
 
    .EXAMPLE
        $string = 'A string'
        $stream = [System.IO.MemoryStream]::new()
        $writer = [System.IO.StreamWriter]::new($stream)
        $writer.Write($string)
        $writer.Flush()
 
        ConvertFrom-MemoryStream -MemoryStream $stream -ToString
 
        A string
 
    .EXAMPLE
        $string = 'A string'
        $stream = [System.IO.MemoryStream]::new()
        $writer = [System.IO.StreamWriter]::new($stream)
        $writer.Write($string)
        $writer.Flush()
 
        $stream | ConvertFrom-MemoryStream -ToString
 
        A string
 
    .EXAMPLE
        $string1 = 'A string'
        $stream1 = [System.IO.MemoryStream]::new()
        $writer1 = [System.IO.StreamWriter]::new($stream1)
        $writer1.Write($string1)
        $writer1.Flush()
 
        $string2 = 'Another string'
        $stream2 = [System.IO.MemoryStream]::new()
        $writer2 = [System.IO.StreamWriter]::new($stream2)
        $writer2.Write($string2)
        $writer2.Flush()
 
        ConvertFrom-MemoryStream -MemoryStream $stream1,$stream2 -ToString
 
        A string
        Another string
 
    .EXAMPLE
        $string1 = 'A string'
        $stream1 = [System.IO.MemoryStream]::new()
        $writer1 = [System.IO.StreamWriter]::new($stream1)
        $writer1.Write($string1)
        $writer1.Flush()
 
        $string2 = 'Another string'
        $stream2 = [System.IO.MemoryStream]::new()
        $writer2 = [System.IO.StreamWriter]::new($stream2)
        $writer2.Write($string2)
        $writer2.Flush()
 
        $stream1,$stream2 | ConvertFrom-MemoryStream -ToString
 
        A string
        Another string
 
    .OUTPUTS
        [String[]]
 
    .LINK
        http://convert.readthedocs.io/en/latest/functions/ConvertFrom-MemoryStream/
#>

function ConvertFrom-MemoryStream
{
    [CmdletBinding(
        HelpUri = 'http://convert.readthedocs.io/en/latest/functions/ConvertFrom-MemoryStream/',
        DefaultParameterSetName = 'ToString'
    )]
    param
    (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [System.IO.MemoryStream[]]
        $MemoryStream,

        [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF7', 'UTF8')]
        [String]
        $Encoding = 'UTF8',

        [Parameter(ParameterSetName = 'ToString')]
        [Switch]
        $ToString,

        [Parameter(ParameterSetName = 'ToBase64')]
        [Switch]
        $ToBase64
    )

    begin
    {
        $userErrorActionPreference = $ErrorActionPreference
    }

    process
    {
        foreach ($m in $MemoryStream)
        {
            try
            {
                $string = ConvertFrom-MemoryStreamToString -MemoryStream $m -ErrorAction Stop

                switch ($PSCmdlet.ParameterSetName)
                {
                    'ToBase64'
                    {
                        ConvertFrom-StringToBase64 -String $string -Encoding $Encoding
                    }

                    'ToString'
                    {
                        $string
                    }
                }
            }
            catch
            {
                Write-Error -ErrorRecord $_ -ErrorAction $userErrorActionPreference
            }
        }
    }
}