Skip to main content

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 = objshell.run(strCommand,0,True)
if strError then
                        
    WScript.Echo "Failed with Exit Code" & strError & "."
else
    WScript.echo "Success"
endif


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 = objshell.run(strCommand,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 ...you 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 modification....like changing the website name to "WebsiteDoesnotExist" and run the VBS...it 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 = objshell.run(strCommand,1,True)
If strError then
                        
    WScript.Echo "Failed with Exit Code" & strError & "."
else
    WScript.echo "Success"
endif


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.


Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell : Trust network share to load modules & ps1

Problem Do you have a central network share, where you store all the scripts or PowerShell modules ? What happens if you try to run the script from a network share ? or if you have scripts (local) which invoke scripts or import PowerShell modules stored on this network share ? Well you would see a security warning like below (Note - I have set execution policy as 'Unrestricted' not 'bypass' here): Run a .ps1 from the network share Well this is a similar warning, which you get when you download scripts from Internet. As the message says run Unblock-File cmdlet to unblock the script and then run it, let's try it.