{"id":5828,"date":"2013-03-12T05:00:00","date_gmt":"2013-03-12T13:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=5828"},"modified":"2013-03-12T00:52:22","modified_gmt":"2013-03-12T08:52:22","slug":"jazz-up-your-vbscripts-with-powershell-and-windows-forms","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2013\/03\/12\/jazz-up-your-vbscripts-with-powershell-and-windows-forms\/","title":{"rendered":"Jazz up your VBScripts with PowerShell and Windows Forms"},"content":{"rendered":"<p>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\u2019t even heard of, this will work too. And best of all, anything I will show you here is free.<\/p>\n<p>Let\u2019s 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\u2019s inputbox. (Disclaimer: I intentionally ignore HTAs) That requires a user to type a name,\u00a0 which can be error prone, considering some machine naming conventions we have seen. Wouldn\u2019t it be nice if you could just<\/p>\n<ul>\n<li>quickly draw a dialog<\/li>\n<li>fill a list with server names<\/li>\n<li>popup a dialog<\/li>\n<li>have the user select a name from the list<\/li>\n<li>get the selected name in your VBScript and continue on<\/li>\n<\/ul>\n<p>Nice, right? Let\u2019s do it, it takes less than five minutes.<\/p>\n<p>You need:<\/p>\n<ol>\n<li>PrimalForms CE (free)<\/li>\n<li>ActiveXPowerShell (free)<\/li>\n<\/ol>\n<p>You can download both from the SAPIEN Downloads site. I\u2019ll get you the links later. All the files from this post will be available too.<\/p>\n<p>First we whip up the form:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/PrimalFormsCE.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"PrimalFormsCE\" alt=\"PrimalFormsCE\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/PrimalFormsCE_thumb.png\" width=\"644\" height=\"451\" border=\"0\" \/><\/a><\/p>\n<p>There isn\u2019t 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\u2019ll fill the list box.<br \/>\nExport this to a PS1 file. Load it into your favorite PowerShell editor because we need to make some slight modifications (PrimalPad is a great free tool for that).<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/PostEdit.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"PostEdit\" alt=\"PostEdit\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/PostEdit_thumb.png\" width=\"644\" height=\"432\" border=\"0\" \/><\/a><\/p>\n<p>I\u2019ll be using PrimalScript here. First the FormLoad handler:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image8.png\" width=\"551\" height=\"241\" border=\"0\" \/><\/p>\n<p>You see it\u2019s pretty simple. We named the list box <em>ServerListCtrl<\/em> in our PFF<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image9.png\" width=\"269\" height=\"134\" border=\"0\" \/><\/p>\n<p>so that means it\u2019s called <em>$ServerListCtrl<\/em> in our generated script.<\/p>\n<p>In our OK button handler we get the selected name and assign it to a global variable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image10.png\" width=\"516\" height=\"81\" border=\"0\" \/><\/p>\n<p>In our Cancel button handler we make sure the global variable is empty, to indicate the user didn\u2019t select anything.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image11.png\" width=\"314\" height=\"84\" border=\"0\" \/><\/p>\n<p>Last, but not least, we declare the global variable on top of the script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image12.png\" width=\"340\" height=\"112\" border=\"0\" \/><\/p>\n<p>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\u2019d give this variable a script scope it would no longer exist once the script has terminated.<\/p>\n<p>Ok, save that file and now we head on over to our VBScript file.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image13.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb6.png\" width=\"644\" height=\"243\" border=\"0\" \/><\/a><\/p>\n<p>This is a pretty simple test script but it gets the job done.<\/p>\n<p>We create an instance of our ActiveXPosh control, execute the script and query for the variable. It\u2019s as simple as that. If you run it, it looks like this:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image14.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb7.png\" width=\"644\" height=\"350\" border=\"0\" \/><\/a><\/p>\n<p>Now have fun jazzing up your old scripts with new user interfaces.<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:48ad4b4d-383d-4b60-b747-803e77d4b7b7\" style=\"margin: 0px; display: inline; float: none; padding: 0px;\">\n<p>Download the sample files here: <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/VBScriptUIs4.zip\" target=\"_blank\">VBScriptUIs.zip<\/a><\/p>\n<\/div>\n<p>You can download ActiveXPowerShell from <a href=\"http:\/\/www.sapien.com\/downloads\">http:\/\/www.sapien.com\/downloads<\/a>. You will find in in the \u201c<em>Free components<\/em>\u201d folder.<\/p>\n<p>PrimalForms CE is in the \u201c<em>Free Tools\\32 Bit<\/em>\u201d folder.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,485,283,509,24,25],"tags":[814,270,28,1016,314,486,37,996,815],"class_list":["post-5828","post","type-post","status-publish","format-standard","hentry","category-ask-the-experts","category-community-tools","category-howto","category-primalforms-software-news","category-vbscript","category-windows-powershell","tag-activexposh","tag-inputbox","tag-powershell","tag-powershell-studio","tag-primalforms","tag-primalpad","tag-primalscript","tag-vbscript","tag-windows-forms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=5828"}],"version-history":[{"count":6,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5828\/revisions"}],"predecessor-version":[{"id":5849,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5828\/revisions\/5849"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=5828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=5828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=5828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}