Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created October 29, 2019 15:59
Show Gist options
  • Save matthewoestreich/a88193e566d211d87ab850bcfd5d56e1 to your computer and use it in GitHub Desktop.
Save matthewoestreich/a88193e566d211d87ab850bcfd5d56e1 to your computer and use it in GitHub Desktop.
Good for converting .dll files into Base64 strings, in order to embed .dll files into scripts. This allows you to either export the .dll to disk, or attempt to run from memory.
function ConvertFileTo-Base64 {
param(
[Parameter(Mandatory=$true)]
[string] $SourceFilePath,
[Parameter(Mandatory=$true)]
[string] $TargetFilePath
)
$SourceFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($SourceFilePath)
$TargetFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($TargetFilePath)
$bufferSize = 9000 # should be a multiplier of 3
$buffer = New-Object byte[] $bufferSize
$reader = [System.IO.File]::OpenRead($SourceFilePath)
$writer = [System.IO.File]::CreateText($TargetFilePath)
$bytesRead = 0
do {
$bytesRead = $reader.Read($buffer, 0, $bufferSize);
$writer.Write([Convert]::ToBase64String($buffer, 0, $bytesRead));
} while ($bytesRead -eq $bufferSize);
$reader.Dispose()
$writer.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment