login vsi company logo login vsi company logo 250x40
  • 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.

 

Tags: Microsoft, Windows, Login AM

Popular Blogs

login-vsi-vdi-performance-summit

The VDI Performance Summit - Virtual Conference and Expo

Visit the VDI Performance Summit to gain knowledge and experience about performance and tuning VDI, improving End-User Experience and IT service. Join us at the ONLY virtual event 100% dedicated to VDI performance and tuning | May 2, 2019 This 1-day event offers key-notes presented by the best VDI performance experts in the world, technical and business oriented breakout sessions, the possibility to chat with experts directly to discuss your own situation, and a virtual exhibit hall featuring… Continue Reading

Scalability testing Parallels Remote Application Server with Login VSI

Recently I went to VMworld in Barcelona where Login VSI had a booth on the expo... While I can’t remember exactly how many conversations I had - there had been so many that I lost my voice on day one. What was new this year is that quite a few people asked if our software is compatible with the solutions from Parallels specifically their Remote Application Server (RAS) (Datasheet). Continue Reading
Login VSI Blog Article - Microsoft Windows 10 Default FTA Associations - Teaser Image

Windows 10 Default File Type Associations and Login VSI

When Login VSI 4.1 was released, the majority of desktops were running Windows 7 and life was easy. We’d set the default filetype for an application and it would simply work. The default and industry standard workloads in Login VSI include launching and using Adobe Reader as part of the virtual user simulation. Because Login VSI doesn’t always know which version of Adobe Reader is installed, or where it’s installed, the workload relies on the file type association (FTA) for .pdf documents to be… Continue Reading
Login VSI - Press Release - IGEL - Login VSI Partner to Optimize End User Computing Experience Image

[Press Release] IGEL Partners with Login VSI to Optimize the End User Computing Experience

Login PI enables organizations to better protect the performance and availability of their IGEL OS-powered virtual desktop environments San Francisco, USA, Feb. 6, 2019: IGEL, a world leader in software-defined endpoint optimization and control solutions for the secure enterprise, today announced that it is partnering with Login VSI, provider of software solutions to test and actively monitor the performance and availability of virtual desktop environments, including VDI and… Continue Reading
Login VSI - Blog - Login PI Blog Teaser Image - Windows Virtual Desktop: How To Monitor User Experience With Login PI

Windows Virtual Desktop – How to Monitor User Experience?

Microsoft has just announced the public preview of their new Windows Virtual Desktop (WVD) offering at Microsoft Ignite on Tour in Amsterdam today. For those of you who’ve not followed the rumors or the private beta, here’s the outline... Continue Reading
Investigating Online Application Performance with Login PI

Investigating Online Application Performance with Login PI

As many companies do, we use a CRM system. Recently, I have been getting complaints about our cloud CRM system, Microsoft Dynamics, being slow. I tried to investigate this by shadowing one of our users to see what was wrong. As expected, everything was fast. 15 minutes later, the same user reported slowness again. How could I investigate this without bothering the users? Continue Reading
Cookie Settings

Didn't find what you need?

Get in touch with your questions and one of our specialists will reply ASAP.

Ask here