login vsi company logo login vsi company logo 250x40

 

Get the best possible VDI performance, density and availability with Login VSI
Our Solutions

  • Home
  • Blog
  • How To: Embedding External Files in PowerShell Scripts

How To: 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.

Login VSI - How-To: Embedding External Files in PowerShell Scripts - 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.

Login VSI - How-To: Embedding External Files in PowerShell Scripts - encoded text file

Encoded text file

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

Login VSI - How-To: Embedding External Files in PowerShell Scripts - 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.

 


 

Start using Login VSI today

Our industry-standard software is built to help you avoid problems, lower costs and improve performance. Request a Quote or get your (free!) Trial below, and benefit from our award-winning services.

Request a Quote  Request a Trial

 

 


About the company Login VSI

The company Login VSI provides end-user performance insights for virtualized desktop and server based computing environments. Enterprise IT departments use flagship product Login VSI (for load testing) and new addition Login PI (for continuity testing) in all phases of their virtual desktop deployment—from planning to deployment to change management—to build and safeguard a good performance, a high availability, and (as a result) a good and consistent end-user experience. For more information about Login VSI or for a free test license contact us.

 

 


 

Start using Login VSI today

Our industry-standard software is built to help you avoid problems, lower costs and improve performance. Request a Quote or get your free Trial below, and benefit from our award-winning services.

Free Price Request  Your Free Trial

 


About the company Login VSI

The company Login VSI provides end-user performance insights for virtualized desktop and server-based computing environments. Enterprise IT departments use flagship product Login VSI (for load testing) and Login PI (for continuity testing) in all phases of their virtual desktop deployment—from planning to deployment to change management—to build and safeguard a good performance, a high availability, and (as a result) a good and consistent end-user experience. For more information about Login VSI or for a free test license contact us.

Tags: Microsoft, Windows, Login AM

Popular Blogs

Login VSI Blog - Ongoing Effects CPU Flaws

The Ongoing Effects of Intel CPU Flaws

The Ongoing Effects of Intel CPU Flaws Over the last year, we’ve seen many Intel CPU hardware flaws come to light and when news first broke about Meltdown & Spectre there was a lot of panic: "should we patch?", "What’s the performance impact?", "Can we still rely on this hardware?" Continue Reading
Login VSI Blog - Updating to Windows 10 1903? Make Sure to Test!

Updating to Windows 10 1903? Make Sure to Test!

Updating to Windows 10 1903? Make Sure to Test! It’s that time of the year again. Microsoft have released their spring update and the first IT-Pro’s are getting ready to move their VDI environment over. Continue Reading
Login AT Express offers Application Compatibility Testing for up to 50 applications for FREE

[Press Release] Login VSI introduces free license for award winning solution, Login AT

Login AT Express offers Application Compatibility Testing for up to 50 applications for FREE Atlanta, USA, May 22, 2019: Login VSI, the industry standard for VDI and SBC performance testing, announced today the availability of a new, free version of Login AT for Bulk Application Compatibility Testing. Continue Reading
Login VSI Blog - LVTE 2109 - Technology Advocates & Experts

Login VSI Technology Advocates and Experts 2019

About a year ago Login VSI, the industry standard in VDI performance testing, announced a new program Recognizing those individuals that have built and displayed extensive knowledge of the Login VSI software solutions, and visibly contribute to the End-User Computing (EUC) Community. Continue Reading
Introducing Login PI 3.4

Introducing Version 3.4 of Login PI

What’s New & Different - Introducing Version 3.4 of Login PI We've updated Login PI, the benchmark in unplanned-change and gradual-deterioration detection for VDI. New features include improving the way information is collected & distributed, the way customized workloads are created & the way actual tests are executed. Continue Reading
Login VSI Blog - What's New in Login PI 3.5

What's New in Login PI 3.5?

What's New in Login PI 3.5? Netscaler, SLA Reporting, Event Logging features and so much more! At the end of June, we released a new version of Login PI 3 bursting with new features and enhancements. There are so many that I could write pages of blog about it, but let’s just keep it simple and focus on some of the big items. Continue Reading
Cookie Settings