In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
Project site here: http://ps2exe.codeplex.com – It’s ALPHA in the current version 0.1.1.0.
Version Update from 0.1.0.0 to 0.1.1.0 at 2011-08-22.
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Call the script with this parameters:
-inputFile PowerShell script file -outputFile file name (with path) for the destination EXE file -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file -verbose (switch) shows also verbose informations – if any.
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”. -extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed. -wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed. -end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
I create a script file containing this line of code:$args | Write-Host
I save it as “c:test2.ps1” and convert it as EXE by using PS2EXE:
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
- Sign the script inside the EXE with code signature
- Sign the EXE with code signature
- Compress the script inside the EXE
- Improve the PSHost implementation inside the EXE.