{"id":13671,"date":"2011-06-10T00:01:00","date_gmt":"2011-06-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/10\/use-powershell-to-leverage-existing-vbscript-scripts\/"},"modified":"2011-06-10T00:01:00","modified_gmt":"2011-06-10T00:01:00","slug":"use-powershell-to-leverage-existing-vbscript-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-leverage-existing-vbscript-scripts\/","title":{"rendered":"Use PowerShell to Leverage Existing VBScript Scripts"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell and still leverage your existing VBScript scripts.\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 5<\/b><\/h3>\n<p>Note: This is part four of a five part series of articles about interacting with the legacy. <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. <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/08\/use-powershell-to-pass-parameters-to-vbscript.aspx\">Part three<\/a> talked about passing parameters to legacy commands, and in <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/09\/use-console-apps-to-supercharge-powershell.aspx\">part four<\/a> we talked about using legacy commands in a Windows PowerShell environment.\nThe past few days, we have shown examples of how Windows PowerShell will interact will legacy systems quite happily. Now we are going to show you a practical example. You see, today the memo&hellip;the BIG memo&hellip;came through the system: &ldquo;We&rsquo;re deploying Exchange Server 2007.&rdquo;\nYou have two reactions:\n&ldquo;WOOOHOOOOOO!! We&rsquo;re moving to Exchange Server 2007! I can&rsquo;t wait! I can&rsquo;t wait!&rdquo;\nThen, of course, it hits. Project plans, budgets, time, sleep (or lack of)&mdash;and the most important bit bursts out of your mouth, &ldquo;What do you mean I can&rsquo;t manage it with VBScript?&rdquo;\nUp until this point, you had an entire ecosystem of servers running just fine&mdash;BATS on the left and VBScript on the right.\nYou almost swore a blue streak. Then you caught my series of blogs on <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/\">Hey! Scripting Guy<\/a>, and your blood settled down. &ldquo;Windows PowerShell will work the Legacy,&rdquo; they say. You read up, and decided to take a look at the situation.\nYour original script for creating new users with Dsquery.exe, and then populating them in Exchange with your special VBScript script looked a little like this:<\/p>\n<blockquote><p>  &#8212;&#8212;&#8212;&#8212;&#8212;- <strong>NEWUSER.CMD<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;- <\/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\">@echo off <br \/>Rem our Target OU for the User <br \/>SET ROOTDN=OU=Branches,DC=CONTOSO,DC=Local<\/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\">rem Get the Name of the User<\/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\">SET \/P First=&#8221;First Name:&#8221; <br \/>SET \/P Last=&#8221;Last Name :&#8221;<\/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\">REM Build our Distinguished DN to add under Our Branches OU<\/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\">SET DOMAIN=CONTOSO<\/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\">SET DN=CN=%First% %Last%,%ROOTDN%<\/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;color: #000000\"><span>SET SAMID=%First:~0,1%%Last% <br \/><\/span><\/span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">SET UPN=%SAMID%@techdays.local<br \/>SET DISPLAY=%First% %Last%<\/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\">REM Create user <br \/>DSADD USER &#8220;%FULLDN%&#8221; -samid &#8220;%SAMID%&#8221; -upn &#8220;%UPN%&#8221; -fn &#8220;%FIRST%&#8221; -ln &#8220;%LAST%&#8221; -display &#8220;%DISPLAY%&#8221; -pwd *<\/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\">REM Create Email&nbsp; <br \/>CSCRIPT.EXE NEWMAIL.VBS %SAMID% %UPN% %DISPLAY% %DOMAIN%<\/span><\/span><\/span><\/p>\n<p>  <span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">IF %ERRORLEVEL% NEQ 0 ( ECHO Failed to Create Email address.&nbsp; Check Logs ) ELSE (ECHO Successfully Created Email)<\/span><\/span><\/span>  <\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;- <strong>NEWUSER.CMD<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;- <\/p>\n<\/blockquote>\n<p>It was a simple, but effective, script that you ran for each new user. It worked fine for Exchange Server 2003 with the VBScript portion, so why can&rsquo;t we retrofit it for this new server that needs Windows PowerShell?\nWe can!\nFirst, let us enable a user in the new Exchange Server 2007 console to get a sample cmdlet. We&rsquo;ll use &ldquo;Mr. Trouble,&rdquo; who has been pestering us for an email address. Today we gave him one. (It does not matter that we assigned all the Junk email to his address, does it?)<\/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\">Enable-Mailbox -Identity &#8216;Contoso.local\/Staff\/Mister.Trouble&#8217; -Alias &#8216;Mister.Trouble&#8217; -Database &#8216;SRV-Exch2007First Storage GroupMyExchDB&#8217;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The sample shows that the identity follows a canonical name. When we write the following code:<\/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-HELP ENABLE-MAILBOX &ndash;examples<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>It shows us that it can also take the following format:<\/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\">DOMAINUsername<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Because our original script is providing the <b>Domain<\/b> name as well as a <b>SamID<\/b>, we can leverage that instead in Windows PowerShell.\nFirst we need to accept those parameters that we were going to pass to VBScript. Maybe we will not use them all, but for the moment, let us match them up as follows:<\/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\">param($SamID,$UPN,$Display,$Domain)<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Now we will construct the <b>Identity<\/b> and switch some of those &ldquo;static&rdquo; values from the sample script:<\/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\">$Identity=$Domain+&rsquo;&rsquo;+$SamID <br \/>$Alias=$SamID <br \/>$MyExchangeDatabase=&#8217;SRV-Exch2007First Storage GroupMyExchDB&#8217;<\/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\">Enable-Mailbox &ndash;Identity $ID &ndash;Alias $Alias -Database $MyExchangeDatabase<\/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\">and we should also check for an error and send the status back<\/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\">IF ($ERROR) Return 13<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Put it all together and save it as NEWMAIL.PS1 in the same directory as your NEWUSER.CMD script, then drop this into your Exchange Server:<\/p>\n<blockquote><p>  &#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <strong>NEWMAIL.PS1<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/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\">param($SamID,$UPN,$Display,$Domain)<\/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\">$Identity=$Domain+&rsquo;&rsquo;+$SamID <br \/>$Alias=$SamID <br \/>$MyExchangeDatabase=&#8217;SRV-Exch2007First Storage GroupMyExchDB&#8217;<\/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\">Enable-Mailbox &ndash;Identity $ID &ndash;Alias $Alias -Database $MyExchangeDatabase<\/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\">IF ($ERROR) Return 13<\/span><\/span><\/span><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; <strong>NEWMAIL.PS1<\/strong> &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<\/blockquote>\n<p>Now, replace the following original line:<\/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\">CSCRIPT.EXE NEWMAIL.VBS %SAMID% %UPN% %DISPLAY% %DOMAIN%<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>with this line:<\/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\">POWERSHELL.EXE &ndash;file NEWMAIL.PS1 &ndash;executionpolicy RemoteSigned %SAMID% %UPN% %DISPLAY% %DOMAIN%<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>And life is good!\nNow, you thought I was about to say, &ldquo;Done!&rdquo; But we do have another option to leverage. What if the Exchange Server is running Windows PowerShell 2.0, and you have remoting enabled? You can leverage that too. A slight change to our Windows PowerShell script:<\/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\">param($SamID,$UPN,$Display,$Domain)<\/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\">$Session=NEW-PSSESSION &ndash;computername SRV-Exch2007<\/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\">$Identity=$Domain+&rsquo;&rsquo;+$SamID <br \/>$Alias=$SamIDf <br \/>$MyExchangeDatabase=&#8217;SRV-Exch2007First Storage GroupMyExchDB&#8217;<\/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\">INVOKE-COMMAND &ndash;Session $Session&nbsp; &#8211; SCRIPTBLOCK { ADD-PSSNAPIN Microsoft.Exchange.Powershell.Addin; Enable-Mailbox &ndash;Identity $ID &ndash;Alias $Alias -Database $MyExchangeDatabase }<\/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\">IF ($ERROR) Return 13<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>You even want to add <b>Get-Credential<\/b>, and possibly combine it with this amazing Hey! Scripting Guy blog by Boe Prox about <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/05\/11\/check-for-admin-credentials-in-a-powershell-script.aspx\">ensuring that you have the proper credentials first<\/a>.\nThere you have a nice example of an existing batch file that is doing its job on a legacy setup and how you can extend it to use on a modern server without breaking your infrastructure. For all the power that is revealed and exposed by Windows PowerShell; it really is just another tool for us to use and leverage as we see fit.\nOh, but what a beautiful tool it is! Remember, the Power of Shell is in you.\nGuest blogger week will continue tomorrow with a surprise guest. A special thank you to Sean for writing this week&rsquo;s blog posts. Hope you enjoyed them. Tune in tomorrow, same batch time, and same batch channel.\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 and still leverage your existing VBScript scripts. 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 be redundant by [&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,45],"class_list":["post-13671","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-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell and still leverage your existing VBScript scripts. 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 be redundant by [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13671","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=13671"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13671\/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=13671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}