This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
ConvertTo-Base64.ps1
102 lines (81 loc) · 3.25 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
###############################################################################################################
# Language : PowerShell 4.0
# Filename : ConvertTo-Base64.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Convert a text (command) to an Base64 encoded string
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Convert a text (command) to an Base64 encoded string
.DESCRIPTION
Convert a text (command) to an Base64 encoded string.
.EXAMPLE
ConvertTo-Base64 -Text 'Set-Location -Path "E:\Temp\Files\";Get-ChildItem'
UwBlAHQALQBMAG8AYwBhAHQAaQBvAG4AIAAtAFAAYQB0AGgAIAAiAEUAOgBcAFQAZQBtAHAAXABGAGkAbABlAHMAXAAiADsARwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA=
powershell.exe -NoProfile -EncodedCommand "UwBlAHQALQBMAG8AYwBhAHQAaQBvAG4AIAAtAFAAYQB0AGgAIAAiAEUAOgBcAFQAZQBtAHAAXABGAGkAbABlAHMAXAAiADsARwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA="
Directory: E:\Temp\Files
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/21/2016 5:54 PM 5 File_01.txt
-a---- 8/20/2016 12:54 AM 9 File_02.txt
-a---- 8/20/2016 12:08 AM 14 File_03.txt
-a---- 6/24/2016 5:01 PM 120 File_04.zip
-a---- 8/20/2016 12:54 AM 14 File_05.txt
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/ConvertTo-Base64.README.md
#>
function ConvertTo-Base64
{
[CmdletBinding(DefaultParameterSetName='Text')]
param(
[Parameter(
ParameterSetName='Text',
Mandatory=$true,
Position=0,
HelpMessage='Text (command), which is to be converted to a Base64 encoded string')]
[String]$Text,
[Parameter(
ParameterSetName='File',
Mandatory=$true,
Position=0,
HelpMessage='Path to the file where the text (command) is stored, which is to be converterd to a Base64 encoded string')]
[String]$FilePath
)
Begin{
}
Process{
switch ($PSCmdlet.ParameterSetName)
{
"Text" {
$TextToConvert = $Text
}
"File" {
if(Test-Path -Path $FilePath -PathType Leaf)
{
$TextToConvert = Get-Content -Path $FilePath
}
else
{
throw "No valid file path entered... Check your input!"
}
}
}
try{
# Convert plain text to bytes
$BytesToConvert = [Text.Encoding]::Unicode.GetBytes($TextToConvert)
# Convert Bytes to Base64
$EncodedText = [Convert]::ToBase64String($BytesToConvert)
}
catch{
throw
}
if($EncodedText.Length -gt 8100)
{
Write-Warning -Message "Encoded command may be to long to run via ""-EncodedCommand"" of PowerShell.exe"
}
$EncodedText
}
End{
}
}