Four ways to package a non-GUI PowerShell script as an executable file
- Details
- Written by Max Trinidad
- Last Updated: 04 November 2019
- Created: 27 June 2019
- Hits: 8126
In this article I will show four Script Packager options available for converting a non-GUI PowerShell script to a packaged executable that can be run from a console shell.
The "Packager" option is available under the "Deploy" ribbon tab in the PowerShell Studio and PrimalScript editors:
Let’s take a look at the following script engine options:
- SAPIEN PowerShell Vx Host (Command Line)
- SAPIEN PowerShell Vx Host (Windows Application)
- SAPIEN PowerShell Vx Host (Windows)
- SAPIEN PowerShell Vx Host (Silent)
I will create the package script executable for each of the above options and check their behavior when running them from the console.
Sample WMI Script
The following PowerShell script uses WMI classes to retrieve some system information and display the results in the console.
The script has two parameters:
- A "-ComputerName" parameter which can handle one or more string values.
- A "-Help" parameter so it can display the Help information about the executable.
See the script below:
The If-Statement logic will determine how the executable acts based on the selected parameter. It will include the necessary string manipulation to convert the string value to an array object if necessary.
Because argument values cannot be a .NET object type, all values passed to the parameters need to be defined as string.
The script also includes a function that will write entries to the system Application Event Log.
Script Engine Options
SAPIEN PowerShell Vx Host (Command Line)
This option will turn the script into a command line executable file that can be run from the console prompt. Also, it can display the command prompt window when executed directly.
To create the script executable, here are the steps to follow using the "Packager Settings".
For more detailed information, reference this article.
- Script Engine – Select "SAPIEN PowerShell Vx Host (Command Line)"
Notice that each script engine highlighted will provide a description and preview.
- Build Commands – Include any Pre-build and/or Post-build commands to be used while preparing the script executable.
After completing the "Packager Settings", select "Build" to generate the script executable. The progress of the build will be display in the "Tool Output" panel.
We are now ready to open a console prompt and run the script executable file. But first, let’s run the following command to display the help information using the "-Help True" parameter.
PS > .\Get-SystemInfo.exe -Help True
Next, to get some system information we will use the "-ComputerName" parameter:
PS > .\Get-SystemInformation -ComputerName "SAPIEN01,165.111.01.1"
The results will be displayed in the console and the system information will be added as an entry in the system Application Event Log.
SAPIEN PowerShell Vx Host (Windows Application)
This script engine option will build the script executable that will run as a Windows application.
Complete the "Packager Settings" sections as shown above, and then select the "Packager" > "Build" option to create the script executable file.
Next, in the console run the script executable with the "-Help True" parameter to display the Help information:
PS > .\Get-SystemInfo.exe -Help True
Then, use the "-ComputerName" parameter to get some system information:
PS > .\Get-SystemInformation -ComputerName "SAPIEN01,165.111.01.1"
Notice that all results will be displayed inside the Windows Application, and the system information found will be added as an entry in the system Application Event Log.
SAPIEN PowerShell Vx Host (Windows)
This option will build the script executable that will run a Windows Dialog box for displaying both input and output values.
After completing the steps for building the script executable file it will be ready to run in the console.
Let’s run the script executable and use the "-Help True" parameter to display the help information:
PS > .\Get-SystemInfo.exe -Help True
Now, using the "-ComputerName" parameter to display system information, each of the system object properties found will show a dialog box for each one.
PS > .\Get-SystemInformation -ComputerName "127.0.0.1"
And again, the script will write the system information as an entry in the system Application Event Log.
SAPIEN PowerShell Vx Host (Silent)
This script engine option will suppress any output sent to the console, and no window is displayed when executed directly.
In the script sample, using the "-Help True" parameter, the help output is suppressed which makes the parameter useless.
Using the "-ComputerName" parameter, the system information output is also suppressed, except when it sends the results to the system Application Event Log.
In Summary
The "Script Engines" options demonstrated above are available in both PowerShell Studio and PrimalScript.
Use either the "SAPIEN PowerShell Vx Host (Command Line)" or "SAPIEN PowerShell Vx Host (Windows Application)" for creating an executable using a PowerShell script. On the other hand, there could be some opportunities for using the "SAPIEN PowerShell Vx Host (Windows)" and "SAPIEN PowerShell Vx Host (Silent)".
Just be creative!
Sample Scripts can be downloaded: PackagedScript.zip
Resource Information
- Parsing Parameters for a Script in an Executable File
- Passing Parameters to a Script in an Executable File
- Displaying Help for a Script in an Executable File
- How to Use PowerShell to Write to Event Logs
- Creating a Windows Application with PowerShell
- Video - Four ways ro package a non-GUI PowerShell script as an executable file
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.