Jazz up your VBScripts with PowerShell and Windows Forms
- Written by Alexander Riedel
- Last Updated: 21 April 2016
- Created: 12 March 2013
- Hits: 2630
Sure, you are supposed to write all new scripts in PowerShell. Some say you can replace any VBScript with a single line in PowerShell. But the truth is, many of you still have to maintain, develop and debug scripts written in VBScript. Some of our customers also have to maintain VBScript or JScript files that run under a custom host, automating some server application that likely will never be changed to use PowerShell. And, judging from some emails we get, you cast a jealous look at this new fangled Windows Forms stuff the PowerShell crowd uses to make great tools. Well, fear not, you can too. The following applies to any scripting language that can use COM objects even through our example uses VBScript. So if you use Perl, AutoIt, JScript, Haskell or some other language I haven’t even heard of, this will work too. And best of all, anything I will show you here is free.
Let’s assume you have this script that queries a network server for disk utilization. All you have available in VBScript to prompt for a server name is VBScript’s inputbox. (Disclaimer: I intentionally ignore HTAs) That requires a user to type a name, which can be error prone, considering some machine naming conventions we have seen. Wouldn’t it be nice if you could just
- quickly draw a dialog
- fill a list with server names
- popup a dialog
- have the user select a name from the list
- get the selected name in your VBScript and continue on
Nice, right? Let’s do it, it takes less than five minutes.
- PowerShell Studio
- ActiveXPowerShell (free)
You can download a trial version of Powershell Studio from its product page and ActiveXPowerShell from the SAPIEN Downloads site. (Links are at the bottom of the article.) All the files from this post will be available too.
First we whip up the form:
There isn’t much to it, a simple form with a label, a list box and two buttons. You need a click handler for the buttons and FormLoad handler where we’ll fill the list box. Export this to a PS1 file. Load it into your favorite PowerShell editor because we need to make some slight modifications (PrimalPad is a great tool for that).
I’ll be using PrimalScript here. First the FormLoad handler:
You see it’s pretty simple. We named the list box ServerListCtrl in our PFF
so that means it’s called $ServerListCtrl in our generated script.
In our OK button handler we get the selected name and assign it to a global variable.
In our Cancel button handler we make sure the global variable is empty, to indicate the user didn’t select anything.
Last, but not least, we declare the global variable on top of the script.
Why does it have to be marked as global? Because we are going to run this form as a script, we need to make sure that anything we need to retrieve after the script has finished still exists. So if you’d given this variable a script scope it would no longer exist once the script has terminated.
Ok, save that file and now we head on over to our VBScript file.
This is a pretty simple test script but it gets the job done.
We create an instance of our ActiveXPosh control, execute the script and query for the variable. It’s as simple as that. If you run it, it looks like this:
Now have fun jazzing up your old scripts with new user interfaces.
You can download ActiveXPowerShell from these links: