Thursday, January 16, 2014

Calling PS1 from VBS and Web-Administration without the PowerShell Module - Part1

Calling PS1 from VBS
Recently I got this strange request to call PowerShell Script from VBScript... Yeah!! I am serious.

Well the scenario is that we have a main VBScript which calls other VBScripts to perform Application Deployments on  Servers.

In this main VBScript we want to add the functionality to start/stop websites and webapppools as these need to be stopped while deploying the web apps on the server. So to add this functionality to our VBS we want to use PowerShell Scripts.

Learning (skip these for now will make more sense after reading the post):

This task seemed to be easy enough at first ...

Sample VBScript 1:

Set objShell = CreateObject("WScript.Shell")
strCommand = "powershell.exe -noprofile -file C:\temp\tes2t.ps1 -websitename DexTestWebsite  -action stop "

strError =,0,True)
if strError then
    WScript.Echo "Failed with Exit Code" & strError & "."
    WScript.echo "Success"

Looks easy enough...we create a WScript.Shell Object and then we just use the run() method on it to run the powershell.exe and wait for it to complete and get back a return code from the process for more details see the documentation of the method here . The VBS will display the exit code returned in case of a failure.

Sample PowerShell Script (test2.ps1)

So my Sample VBS will call this Sample PowerShell Script to stop a website named "DexTestWebsite".....See below :

Well seeing this I knew I had a huge problem at hand...First of all correct Exit code not being sent back and second there was some problem with the Script not able to start/stop the website.

So here is what I did.....I just executed another sample VB Script which opens up a PowerShell Console.

Below is the Sample VBScript 2:

Set objShell = CreateObject("WScript.Shell")
strCommand = "powershell.exe -noexit"

strError =,1,True)

I can then interact with the PowerShell Console create by above VBS and see what happens when I try to Import the WebAdministration Module and run Get-Website cmdlet :

That's a bummer...cause everything works on a normal PowerShell instance (blue window on right).

So I have identified one part as to why this fails....WebAdministration Module can't be used......This is proving to be not so easy after all.

So I googled around and came up with below Script which runs perfectly fine in the PowerShell instance spawned by the VBS can check that by running it on that PowerShell instance:

Below is just the sample of the Script:

Note : You might want to modify above Script if you are looking to use.
Just as a proof that the above Script works for me..below is a screenshot:

So now the big moment...Try calling the first Sample VBScript 1 , Now we will be using the new PS1 (which doesn't use the WebAdministration PowerShell Module). Refer below Screenshot:

Phewwww ! Finally........It works !

But wait there's more which will make you scratch your head.
Suppose to our Sample VBScript 1 we make small changing the website name to "WebsiteDoesnotExist" and run the should report back a failure right. Below is the modified Sample VBScript 1:

Set objShell = CreateObject("WScript.Shell")
strCommand = "powershell.exe -noprofile -file C:\temp\test2.ps1 -websitename WebSiteDoesnotExist -action stop -verbose"

strError =,1,True)
If strError then
    WScript.Echo "Failed with Exit Code" & strError & "."
    WScript.echo "Success"

Ok Let's run this....but let me assure you ...I don't have a website running by that name , So when I run the VBS above:

This keeps getting interesting .......
So after a lot of head-scratching and countless hours of staring at the code and lot of testing....I was able to make this work and I will discuss that in next post....this one is exceeding the "Boring" limit already :P

See ya all soon.

P.S. - I was testing all of this on a Windows Server 2008 R2 machine with IIS 7.5.7600.16385 running and PowerShell version 2.

----------------------UPDATE ------------------------
I tried to reproduce this on my Server 2012 Lab ....But couldn't.
The WebAdministration PowerShell Module works fine on it.