login vsi company logo login vsi company logo 250x40
header-04.jpg
  • Home
  • Blog
  • Embedding External Files in PowerShell Scripts

Embedding External Files in PowerShell Scripts

Embed External Files in PowerShell Scripts

Sometimes you have to create a custom script that requires external files, e.g. binary dll files or executables. One of the ways you can make sure the script is executed succesfully is by creating a component that first copies the necessary files to the correct locations using the FileCopy actionitem, but today I am going to show you how to embed a file in a script using base64 encoding. 

Encoding the file

The file we want to embed is called AM.dll and is located on my development machine in c:\AM\AM.dll.

embedding a file

Embedding a .dll file

To encode the file to base64 we give in the following commands in powershell prompt:

$Content = Get-Content -Path C:\AM\AM.dll -Encoding Byte
$Base64 = [System.Convert]::ToBase64String($Content)
$Base64 | Out-File c:\AM\encoded.txt

The result should be a file called encoded.txt in c:\AM.

encoded text file

Encoded text file

Open the encoded.txt file and you will see a base64 encoded string representation of the dll file.

base64 encoded string

Base64 encoded string

Embedding the file

To embed the file, simply create a new variable in your script which contains the base64 string.

$Base64 = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
..........+DDEAsAA+DDkAsAA+DEEAsAA+DEkAsAA+DFEAsABDDFkAsAA+DGEAsAA+DGkAsAA+DHEAsABIDIEAsABODIkAsAAOAJEA6gYSAJkAAAdTDAkA"

NOTE: The example above has been cropped for readiblity of the blogpost, this should be a rather long string.

Decoding the file

After we have embedded the base64 string representation of the file we need to decode it to a dll file before we can use it.

$Content = [System.Convert]::FromBase64String($Base64)
Set-Content -Path $env:temp\AM.dll -Value $Content -Encoding Byte

Now we can use the $env:temp\AM.dll file in the rest of our script, for housekeeping reasons, remove the dll file at the end of the script.

Remove-Item $env:temp\AM.dll

The entire script would look like

$Base64 = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
..........+DDEAsAA+DDkAsAA+DEEAsAA+DEkAsAA+DFEAsABDDFkAsAA+DGEAsAA+DGkAsAA+DHEAsABIDIEAsABODIkAsAAOAJEA6gYSAJkAAAdTDAkA"
$Content = [System.Convert]::FromBase64String($Base64)
Set-Content -Path $env:temp\AM.dll -Value $Content -Encoding Byte
#Do your things
Remove-Item $env:temp\AM.dll

There you go, a way to embed files in a powershell script, ensuring that the external files the script requires will always be there (with the right version, might I add).

Happy encoding and embedding.

Ps. Also, make sure to read my colleague Mark's blog about cleaning up esxtop log files using PowerShell.

About the author

Tags: Microsoft, Windows, Login AM

What our customers are saying

Keith Hageman - Micron

"When I joined Micron in June 2014, the first thing I did is find out if we have a copy of Login VSI and if we could run a 1000 user test case instance (which we could). We use Login VSI to run tests for customers and for case studies and I really have loved Login VSI for the last 3 or 4 years.”

Keith Hageman, Senior Solutions Architect at Micron Technology


Chris Gebhardt - NetApp

“We use Login VSI to make sure that what we are recommending to our customers is much better than what their guesses are. Because if you don't do proper assessment and if you don’t use Login VSI, your architecture is just a guess.”

Chris Gebhardt, Senior Technical Marketing Engineer at NetApp


Bill Galloway - Pivot3

“The labs at Pivot3 have hundreds of machines. A portion of those are running different Login VSI configurations so we can be constantly configuring with different hardware to see how that affects desktops. Then we tweak the RAM and CPU, and tune so we know how many users we can support.”

Bill Galloway, Founder and CTO at Pivot3

Login VSI, Inc.

300 Tradecenter

Suite 3460
Woburn, MA 01801

Phone: +1 844 828 3693

Login VSI B.V.

De Entree 85
1101 BH Amsterdam
The Netherlands

Phone: +31 20 705 1200