-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConvertFrom-Base64.ps1
85 lines (76 loc) · 2.55 KB
/
ConvertFrom-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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function ConvertFrom-Base64 {
<#
.SYNOPSIS
Convert from a Base64 string to normal string
.DESCRIPTION
Convert from a Base64 string to normal string. Function aliased to 'Base64Decode'.
.PARAMETER Base64
A base64 encoded string
.PARAMETER IncludeInput
Switch to enable including the input to appear in the output
.EXAMPLE
ConvertFrom-Base64 "SABlAGwAbABvAA=="
Would return
Hello
.EXAMPLE
ConvertFrom-Base64 "SABlAGwAbABvAA==" -IncludeInput
Would return
Base64 String
------ ------
SABlAGwAbABvAA== Hello
.OUTPUTS
[string[]]
#>
#region Parameter
[CmdletBinding(ConfirmImpact='None')]
[alias('Base64Decode')] #FunctionAlias
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeLine)]
[string[]] $Base64,
[ValidateSet('ASCII', 'BigEndianUnicode', 'Unicode', 'UTF32', 'UTF7', 'UTF8')]
[string] $EncodingType = 'Unicode',
[switch] $IncludeInput
)
#endregion Parameter
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
} #close begin block
process {
foreach ($curBase64 in $Base64) {
$bytesfrom = [Convert]::FromBase64String($curBase64)
switch ($EncodingType) {
'ASCII' {
$decodedfrom = [System.Text.Encoding]::Ascii.GetString($bytesfrom)
}
'BigEndianUnicode' {
$decodedfrom = [System.Text.Encoding]::BigEndianUnicode.GetString($bytesfrom)
}
'Unicode' {
$decodedfrom = [System.Text.Encoding]::Unicode.GetString($bytesfrom)
}
'UTF32' {
$decodedfrom = [System.Text.Encoding]::UTF32.GetString($bytesfrom)
}
'UTF7' {
$decodedfrom = [System.Text.Encoding]::UTF7.GetString($bytesfrom)
}
'UTF8' {
$decodedfrom = [System.Text.Encoding]::UTF8.GetString($bytesfrom)
}
}
$decodedfrom = [Text.Encoding]::Unicode.GetString($bytesfrom)
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
Base64 = $curBase64
Encoding = $EncodingType
String = $decodedfrom
})
} else {
Write-Output -InputObject $decodedfrom
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}