User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

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.

You need:

  1. PowerShell Studio
  2. 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:

Create simple form in Powershell studio

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).

Load the PS1 file into your favorite editor

I’ll be using PrimalScript here. First the FormLoad handler:

FormLoad Handler

You see it’s pretty simple. We named the list box ServerListCtrl in our PFF

ListBox name

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.

OK Button - 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.

Cancel button - 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.

declare the global variable

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.

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:

create an instance of our ActiveXPosh control, execute the script and query for the variable

Now have fun jazzing up your old scripts with new user interfaces.

Download the sample files here.

You can download ActiveXPowerShell from these links:

You can download the 45 day trial version of PowerShell Studio from this page.

If you have questions about our products, please post in our support forum.
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 Trial Software Questions forum.
Copyright © 2017 SAPIEN Technologies, Inc.