Functions/ConvertTo-Base64.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function ConvertTo-Base64 {
<#
.SYNOPSIS
    ConvertTo-Base64 converts a normal string to a base 64 string
.DESCRIPTION
    ConvertTo-Base64 converts a normal string to a base 64 string. Function
    aliased to 'Base64Encode'.
.PARAMETER String
    The string you want manipulated
.PARAMETER IncludeInput
    Switch to enable input parameters to appear in output
.EXAMPLE
    ConvertTo-Base64 -String 'Password'
 
    Would return
    UABhAHMAcwB3AG8AcgBkAA==
.EXAMPLE
    ConvertTo-Base64 -String Hello,Goodbye -IncludeInput
 
    String Base64
    ------ ------
    Hello SABlAGwAbABvAA==
    Goodbye RwBvAG8AZABiAHkAZQA=
.OUTPUTS
    [string[]]
.LINK
    about_Properties
#>


    [CmdletBinding(ConfirmImpact = 'None')]
    [alias('Base64Encode')]
    param
    (
        [Parameter(ValueFromPipeline)]
        [string[]] $String,

        [switch] $IncludeInput
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        foreach ($curString in $String) {
            $bytesto = [System.Text.Encoding]::Unicode.GetBytes($curString)
            $encodedto = [System.Convert]::ToBase64String($bytesto)
            if ($IncludeInput) {
                New-Object -TypeName psobject -Property ([ordered] @{
                        String = $curString
                        Base64 = $encodedto
                    })
            } else {
                Write-Output -InputObject $encodedto
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }

}