PrimalScript Packaged PrimalForm PowerShell

I hope you’ve been playing with the free PrimalForms tool and started creating some useful PowerShell tools. Here’s a technique you might want to explore if you are using PrimalScript. If you’ve used the PrimalScript for awhile you likely are aware of the script packaging feature. You can use it to package your PowerShell scripts into an EXE file.  Remember though, PowerShell needs to be installed wherever you may want to execute your package.  But once packaged, launching the file will display your WinForm based script.

One thing you’ll notice though is that a PowerShell window is also displayed in the background. This is only natural as PowerShell is required to run the script. If your form is supposed to interact with the PowerShell window this may be fine. But if your form is more of a stand-alone utility, the window may be a bit of a distraction. The script packager has no mechanism to hide the window but here’s one suggestion that might work for you.

I’ve created a VBScript, PSLauncher.vbs, that serves as a wrapper for a PowerShell forms script.

   1: '==========================================================================
   2: '
   3: ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
   4: '
   5: ' NAME: PSLauncher.vbs
   6: '
   7: ' AUTHOR: Jeffery Hicks , SAPIEN Technologies, Inc.
   8: ' DATE  : 11/3/2008
   9: '
  10: ' COMMENT: Use this VBS wrapper to launch a PowerShell Forms script with a 
  11: ' minimized CMD window
  12:  
  13: ' Window Styles
  14: ' 0  Hide the window.
  15: ' 1  Activate and display.
  16: ' 2  Activate and minimize.
  17: ' 3  Activate and maximize.
  18: ' 4  Display window in its last  position.
  19: ' 5  Activate and display the window  in its last position. This is the default.
  20: ' 6  Minimize the window and  activate the next active window.
  21: ' 7  Minimize the window. The active  window remains active.
  22: ' 8  Display the window in its current state. The active window remains active.
  23: ' 9  Activate and display the window. Window is restored to its original state.
  24: ' 10 Set the state based on the  state of the program that launched the command.
  25:  
  26: '
  27: '==========================================================================
  28: Dim oShell
  29: Set oShell=CreateObject("Wscript.shell")
  30:  
  31: 'the name of the script to run
  32: sPSH="c:\temp\driveinfo.ps1"
  33:  
  34: 'the title of your form
  35: sTitle="Drive Report"
  36:  
  37: 'leave this alone unless you want to modify to not load a profile, etc.
  38: sCMD="PowerShell -Command " & CHR(34) & "& {" & sPSH & "}" & CHR(34)
  39:  
  40: 'used for debugging
  41: ' MsgBox sCMD
  42:  
  43: 'run the command with a minimized window
  44: oShell.Run sCMD,2,vbFalse
  45:  
  46: 'you may need to experiment with a sleep interval to give 
  47: 'the script a chance to start
  48: WScript.Sleep 3000
  49:  
  50: 'Activate the window
  51: oShell.AppActivate "Drive Report"
  52: oShell.SendKeys "{ENTER}"

The script uses the Run() method of the Wscript.Shell object to launch your script from a PowerShell prompt.  The wrapper script needs to know the location of the PowerShell script when the package is executed. When you package a script you can specify the location, the wrapper script assumes C:\Temp.

‘the name of the script to run
sPSH=”c:\temp\driveinfo.ps1″

You can’t totally hide the PowerShell window. If you do, your form will be hidden as well. The best I can come up with is to run it minimized.

sCMD=”PowerShell -Command ” & CHR(34) & “& {” & sPSH & “}” & CHR(34)

‘run the command with a minimized window
oShell.Run sCMD,2,vbFalse

The wrapper script sleeps for a few seconds to give the PowerShell script time to launch. You may need to tweak this value depending on your script.

WScript.Sleep 3000

Finally, I use the AppActivate() method to select the form window and bring it to the forefront.

oShell.AppActivate “Drive Report”
oShell.SendKeys “{ENTER}”

This isn’t a perfect solution, but a reasonable compromise.

To make this all work, when you package the script, add the VBS file as the script file and the PowerShell script as a data file. Remember to coordinate the execution path in the package with the path to your PowerShell script in the VBS wrapper.

I’ve attached a ZIP file with a packaged sample, the PSLauncher VBS script and the PowerShell script which you can download here.