Thursday, April 4, 2013

How to work the Command Window using VBScript?


VBScript is ActiveX-enabled scripting language developed by Microsoft that connects to scripting hosts like Internet Explorer and performs functions locally using the Windows Script Host (WSH). It is an interpreted client side language. VBScript uses the Component Object Model (COM) to access elements of the environment within which it is running. Generally, scripting languages are coded faster as these are interpreted. VBScript is structured and used with smaller programs with limited capability. It is embedded within windows by default in every desktop release since Windows 98. 

Want to learn VBScript? Use my free VBScript video tutorials.
A VBScript script must be executed within a host environment, of which there are several provided with Microsoft Windows, including Windows Script Host (WSH), Internet Explorer (IE), and Internet Information Services (IIS). Additionally, the VBScript hosting environment is embeddable in other programs, through technologies such as the Microsoft Script Control (msscript.ocx). VBScript allows us to write small automation utilities and to execute them without using any functional automation tool. We will now learn how to work with command mode.

For command mode, we have to create an object of WScript.Shell class (Window Script Host Shell) and then need to call Run method. Below is code snippet. All the source code is in italics.

Set objShell = CreateObject("WScript.Shell") ' Create object of Shell
objShell.Run "CMD /K" ' Open Command mode

To wait we have to call Sleep method (static method):

Wscript.Sleep(200) ' Wait for 200 milli-seconds

For writing command in CMD, we need to call SendKeys method. E.g.:

objShell.SendKeys("CD \") ' Type CD \ in command mode

For killing all instances of cmd.exe, we need to use following code snippet:

Dim objWMIService, colProcessList, objProcess

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name ='cmd.exe'")

For Each objProcess in colProcessList
   On Error Resume Next
   objProcess.Terminate()
   On Error goto 0
Next

Set objWMIService = Nothing
Set colProcessList = Nothing


If you want to write an example VBScript utility and see how it works, open notepad and type the following code. Replace any executable with cmd.exe in this code to kill all running instances of that executable.
[Important] Keep in mind that killing a process may cause data loss (if data is not saved) in that process. Additionally, killing a process may make your system unstable.

Dim objWMIService, colProcessList, objProcess ' Define variables
Set objShell = CreateObject("WScript.Shell") ' Create object of Shell
objShell.Run "CMD /K" ' Open Command mode

Wscript.Sleep(200) ' Wait for 200 milli-seconds

objShell.SendKeys("CD \") ' Type CD \ in command mode
Wscript.Sleep(200)
objShell.SendKeys("~") ' Used for return key
Wscript.Sleep(200)

objShell.SendKeys("C:")
Wscript.Sleep(200)
objShell.SendKeys("~")
Wscript.Sleep(200)

objShell.SendKeys("CD C:\WINDOWS\System32")
Wscript.Sleep(200)
objShell.SendKeys("~")
Wscript.Sleep(200)

objShell.SendKeys("notepad.exe") ' Open instance of notepad.exe
Wscript.Sleep(200)
objShell.SendKeys("~")
Wscript.Sleep(200)

objShell.SendKeys("VBScript is good") ' Write something in notepad
Wscript.Sleep(200)

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
'Extract collection of all running instances cmd.exe
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name ='cmd.exe'") 

For Each objProcess in colProcessList
   On Error Resume Next
   objProcess.Terminate() ' Terminate the cmd.exe process
   On Error goto 0
Next

Set objWMIService = Nothing
Set colProcessList = Nothing

Set objShell = Nothing

Save notepad file as .vbs extension. Double click .vbs file to execute script. Please provide your feedback. Thanks!

Note: This article has been contributed by Vinod Joshi, who is a test automation professional. Vinod is a respected test automation designer. He is very active in the software testing space community. Additionally, Vinod is the moderator of the Software Testing Space group in LinkedIn.