Guy's Scripting Ezine 80 - ComSpec and CMD
VBScript ComSpec and CMD
This week's Ezine describes how to script relatively simple events at the command line. Even though the tasks are straightforward, the syntax can catch you out, unless you pay attention to the speech marks and ampersands.
When I want a script that will open a Dos box, I cannot make up my mind whether ComSpec is better than plain cmd. As ever, knowledge is power, so let us first find out a little more about ComSpec. Open the System Icon by pressing the Windows key and Pause/Break, navigate to the Advanced Tab, Click on Environmental Variables button, and lo and behold - ComSpec is listed as System Environmental Variable. It turns out to be merely a placeholder for cmd.exe. The nuance is that you could set ComSpec to call another program. To digress, you could even add more environmental variables.
You may ask, 'What is the relevance of ComSpec to scripting?' The answer is for controlling VBScripts that employ .Run commands. My dilemma is that SendKeys in Ezine 26 was very popular and so deservers more attention, on the other hand I worry about SendKeys because one wrong instruction and you could get disastrous results. For example, your intention is to delete all internet temporary files. You navigate to the desired directory and then issue *.*. Imagine your horror if you were in the wrong directory when you issued *.* UGhhhhhhhh.
This Week's Secret is this, by comparing plain objShell.run cmd with fancy objShell.run %ComSpec%, it literally gave me perspective on how to construct scripts which employ dos type commands.
Encouraging computers to sleep when they're not in use is a great idea - until you are away from your desk and need a file on that remote sleeping machine!
WOL also has business uses for example, rousing machines so that they can have update patches applied. My real reason for recommending you download this free tool is because it's so much fun sending those 'Magic Packets'. Give WOL a try - it's free.
Let us pretend that this scriplet is a section in a much bigger script. As part of the design we want to get the Command Prompt section working effectively. The secret of success with %comspec% is to identify the three layers of command:
1) VBScript commands to create
and then run the object:
2) Understanding the switches used by %comspec% or cmd:
3) A reminder of simple dos commands
This script will execute on any Windows machine with WSH and where the ComSpec Environmental exists and is set to cmd.exe.
Instructions for Experimenting with DOS commands
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information on UMRA.
This scriplet seeks to just open the command prompt.
Note 1: From a VBScript point of view see how we create a Wscript.Shell object (and not network object or an Active Directory LDAP:// connection.)
Note 2: ObjShell has about a dozen associated methods, here we need the simple: .run. (Script Editors help you explore other methods.)
Note 3: As ever, play close attention to the syntax, especially the speech marks.
Note 4: Perhaps %username% is the most famous system variables, however, here VBScript employs %comspec%.
Note 5: /k is actually a cmd.exe switch. It means keep the command prompt window open and wait for the next instruction.
Challenges: VBScript Comspec Switches
As this week's scripts are short and relatively straight forward, all the more freedom to experiment.
1: Substitute /c for /k. Not very interesting here, it closes the command prompt window before you see it! Yet in other scripts you don't want the users to see a dos box. Conclusion /c has its place but not in this script.
2: Here is this week's recurring theme, swap cmd for %comspec%. David Wanamaker explains the subtle difference between these two constructions:
%comspec% is a variable that points to the system's command interpreter, which in most cases is cmd.exe. On a default installation of Windows, if you do an 'echo %comspec%' at a command line, you'll see that it gives you the path to cmd.exe which is typically in \windows\system32. That is the reason why using 'cmd' instead of '%comspec%' works because \windows\system32 is normally in the user's path.
%comspec% and cmd.exe are the same thing. It's just better in my opinion to use %comspec% because you are guaranteed that you will use the system's command interpreter. If for some reason cmd.exe was moved to a different location and that different location is not in your path, cmd.exe would fail as a 'command not found' while %comspec% would always point to that different location.
Extra Information Kindly Supplied by Tom Hatfield
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.
Here is where we start to put %comspec% or cmd.exe to work. What we want is more useful output, the ability to manipulate the 'Dos Box' with comspec switches.
Note 1: objShell.Run "%comspec% /k e: & dir". Firstly make sure that you have an e: drive, else change the value to d: or even c:
Note 1a: At the risk of teaching grandfathers to suck eggs, dir means list the directory.
Note 2: Know your Dos. For example, e:\ is 'over think', due to the slash the script does not work as expected. Stick with plain e:.
Note 3: The ampersand (&) acts as a carriage return in the Dos prompt window. Again, you learn more if you omit the &. The result would be a different directory listing, from what you intended.
Note 4: Keep you eye on the speech marks. My example has one pair at the beginning and another at the end, however, there will be other examples with nested command which require more pairs of speech marks.
Here are my challenges for you. Add a few more instructions to the original script.
1) CMD exe has a surprising number of switches, investigate cmd /? and see the list. For example, /d
2) Suppose you have a directory called 'e:\logs', try
3) Add a
command to pipe the output to a text file. dir > guylogs.txt
Perhaps you are bored substituting cmd for %comspec%. (I was by this stage.) Both act in the same way, and I declare them interchangeable.
Note the following two lines of code. See how one command word-wraps at the end of the first line, with the help of underscore (_) and with the aid of an extra ampersand on the second line. Also, here is an example of two non-overlapping sets of speech marks.
"%comspec% /c e: & cd
logs & dir >guylogs.txt "_
ComSpec is ideal for adding command line code to your VBScript. %comspec% and cmd.exe are virtually identical as far objShell.run is concerned. For a short script there are a surprising number of factors involved, VBScript, ComSpec (or cmd.exe) and pure dos commands.
See more about VBScript syntax