{"id":13691,"date":"2011-06-08T00:01:00","date_gmt":"2011-06-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/08\/use-powershell-to-pass-parameters-to-vbscript\/"},"modified":"2011-06-08T00:01:00","modified_gmt":"2011-06-08T00:01:00","slug":"use-powershell-to-pass-parameters-to-vbscript","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-pass-parameters-to-vbscript\/","title":{"rendered":"Use PowerShell to Pass Parameters to VBScript"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to pass parameters to VBScript scripts and to console applications.\nMicrosoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to be redundant by reposting <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/05\/14\/use-the-kiss-principle-for-your-powershell-script.aspx\">his biography<\/a> each and every day.<\/p>\n<h3><b>Integrating Windows PowerShell with Legacy Environments&mdash;Part 3<\/b><\/h3>\n<p>Note: this is part three of a five part series on interacting with legacy systems. <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/06\/get-legacy-exit-codes-in-powershell.aspx\">Part one<\/a> talked about retrieving exit codes, <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/07\/work-with-legacy-exit-codes-in-powershell.aspx\">part two<\/a> discussed working with exit codes.\nAh yes&hellip; where were we? Oh yes. The classroom, the Scripting Guy, a rather sopping wet, energetic student&hellip;\n&hellip;and some Windows PowerShell stories.\nWith a spin that would have challenged the power of most cyclones, the scruffy, mostly bald, little fellow dried himself off.\n&ldquo;Please Scripting Guy, please tell me more. You suggested Windows PowerShell could pass parameters to VBScript scripts and to console applications as well. Oh you must tell us!&rdquo; he chittered like a grey squirrel.\nThe Scripting Guy nodded. &ldquo;Well then, because you are so well behaved this time, I shall finish,&rdquo; he said as he casually tossed our now well-behaved friend a bag of cooked spaghetti. &ldquo;Sit down, relax, and enjoy the rest of the show&hellip;&rdquo;\nThe final script was invoked:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">GET-CONTENT C:PowershellLegacy3.txt | more<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Because we know that console applications and VBScript scripts normally can receive parameters, it isn&rsquo;t such a far stretch to suggest that Windows PowerShell could pass them along as well.\nWe can, but just as when we communicate to Windows PowerShell from the console, we need to be equally aware of some ever so slight differences.\nRemember one thing about Windows PowerShell&hellip;what does it work in all the time? The numbers, the strings, the arrays?\nIt works in objects, not in text. Even if it is working with a string, it is still an object. This means that before we send that data to the console world, we may have to sanitize it a bit. For example, here we call up a batch file within cmd.exe from Windows PowerShell and pass some simple parameters:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value1=&rsquo;Scripting Student&rsquo;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value2=GET-DATE<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value3=$TRUE<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">cmd.exe \/c C:CaveBat1.cmd $value1 $value2 $value3<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>When Windows PowerShell sends this information to the batch file or console application, it sends its &ldquo;best guessed&rdquo; string format (similar to when you use <b>Export-CSV<\/b>) of the same data.\nOur first value, <b>&lsquo;Scripting Student&rsquo;<\/b>, will actually come across to the console application as follows because this is its default output converted as a string:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Scripting Student<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>But <b>$value2<\/b> might come across as follows because this is what its default output will be:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Friday, May 06, 2011 11:34:00 AM<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>What if we wanted it to be something more useful? Say we used <b>Get-Date<\/b> from Windows PowerShell (or an application passed down a [DATETIME]-based .NET output), and we wanted something cleaner&mdash;say only the day, month, and year? We would want to convert it, and then for good measure, possibly tack on a <b>.tostring()<\/b> to ensure that we were only sending string-based output from Windows PowerShell.\nOr perhaps we just want the month? Or we may possibly want to pull only a <b>ShortDate<\/b> out of this information. These are things we need to consider before &ldquo;dumping&rdquo; that data to the console application. Here is an example:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value2.toshortdatestring()<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>In this case, our console application will now receive more sensible text, as shown here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">05\/05\/2011<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Similarly, sending a Boolean $TRUE might be a useless gesture for our console application. Perhaps numbers might be more useful to us? Again, first take the information and choose &ldquo;how&rdquo; it is going over.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">[BYTE]$value3<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">So with a simple change to our values like so<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value1=&rsquo;Scripting Student&rsquo;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value2=(GET-DATE).toshortdatestring()<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$value3=[byte]$TRUE<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">cmd.exe \/c C:CaveBat1.cmd $value1 $value2 $value3<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>We can ensure that not only are we passing parameters to console applications, but that the parameters are useful. Incidentally, we didn&rsquo;t really mention VBScript did we? That&rsquo;s because your VBScript application will receive the data in the exact same manner as the console application. Run it as follows in Windows PowerShell.<\/p>\n<blockquote><p>  <span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">CSCRIPT.EXE VbScriptName.vbs $value1 $value2 $value3<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The only trick to all of this is knowing that when you send data from or receive data to Windows PowerShell, you should prepare it going out or upon receipt.\nThere you have it. You can send and receive data between the different systems, and you can return exit codes.&nbsp;&nbsp;\nWhat does this mean to the administrator? You don&rsquo;t need to scrap your existing infrastructure just to leverage Windows PowerShell.&nbsp; You can simply add the piece that you need to your existing scripts. And similarly, if you have a newer setup with Windows PowerShell, but you have a vendor application that can only be managed by VBScript or that provided console-based executables, you can still work with them from your newer setup.\nThis also means that a newer world has opened up to us. We can take our existing console applications and do new and even more interesting things with them by leveraging Windows PowerShell. And this can only mean one thing for the administrator&hellip;getting home earlier to enjoy life!\n&ldquo;Well?&rdquo; the Scripting Guy looked at his little friend.\nThe formerly fidgety fellow was stunned. His mouth was agape. His eyes were as wide as saucers. He was, for the first time in his life, speechless. He stood up slowly and walked to the Scripting Guy, bowing down to a single knee, &ldquo;Teach me your ways, O master&hellip;&rdquo;\nGuest blogger week will continue tomorrow when Sean will continue to talk about Windows PowerShell and the Legacy. A special thank you to Sean for writing this week&rsquo;s blog posts. Hope you enjoy them.\nI invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to pass parameters to VBScript scripts and to console applications. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,2,3,4,154,155,45],"class_list":["post-13691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to pass parameters to VBScript scripts and to console applications. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13691"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13691\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=13691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}