Embed files in PowerShell Script

Embed files in PowerShell Script

  There are times that you need to include files with a PowerShell script but maybe you don't necessarily want to have several separate files.

PowerShell Web
Create a Portable Encrypted Password
Lost AD Trust and Your Local Account Doesn’t Work!

 

There are times that you need to include files with a PowerShell script but maybe you don’t necessarily want to have several separate files. We can easily convert those files to base64 then embed them into a PowerShell script where it can easily be decoded as needed.

Required Functions:

function ConvertTo-DeflateBase64String{
    [cmdletbinding(DefaultParameterSetName="String")]
    Param(
        [Parameter(ParameterSetName="String",Mandatory=$true,ValueFromPipeline=$true,Position=1)][string]$String,
        [Parameter(ParameterSetName="Byte",Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2)]
        [Byte[]]$Bytes
    )
    process{
        $ms = New-Object System.IO.MemoryStream
        $cwr = New-Object System.IO.Compression.DeflateStream($ms,[System.IO.Compression.CompressionMode]::Compress)

        $sw = New-Object System.IO.StreamWriter($cwr)
        switch ($PSCmdlet.ParameterSetName) {
            "String" { $sw.Write($String) }
            "Byte" { $sw.Write( [char[]]$Bytes) }
            Default { Write-Error "Unknown input format $_"; return}
        }
        $sw.close()

        $compressedBytes = $ms.ToArray()
        $ret = [System.Convert]::ToBase64String($compressedBytes)
        return $ret
    }
}
function ConvertFrom-DeflateBase64String{
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)][string]$String,
        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)]
        [ValidateSet('String','Byte')][String]$OutputFormat = 'String'
    )
    process{
        $data = [System.Convert]::FromBase64String($String)

        $ms = New-Object System.IO.MemoryStream
        $ms.Write($data,0,$data.length)
        [void]$ms.Seek(0,0)
        $cwr = New-Object System.IO.Compression.DeflateStream($ms,[System.IO.Compression.CompressionMode]::Decompress)

        $sr = New-Object System.IO.StreamReader($cwr)
        switch ($OutputFormat) {
            'String' { 
                $ret = $sr.ReadToEnd()
                return $ret
            }
            'Byte' {
                $ReturnBytes = while ($sr.Peek() -gt -1) {
                    [byte]$sr.Read()
                }
                return $ReturnBytes
            }
            Default { Write-Error "Unknown output format $_"; return}
        }
        
    }
}

For example, lets say we have a file called “required.dll” First you would convert the file to base64, to save space lets also compress the base64, and to save time lets add it to our clipboard so we can paste it into our script.

Get-Content require.dll -Raw -Encoding Byte | ConvertTo-DeflateBase64String | Set-Clipboard

Now you paste the contents of your clipboard to where you want in your script, for example:

$dll = "paste-here"

You can then either deflate directly in your script:

$requireddll = ConvertFrom-DeflateBase64String $dll

Or deflate and save back to a file:

ConvertFrom-DeflateBase64String $dll -OutputFormat Byte | Set-Content required.dll -Encoding Byte -NoNewLine

By using the -Encoding parameter this should work with any file type.

COMMENTS

WORDPRESS: 0