Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


December 13, 2004

7 Cool Command-Shell Scripting Tools

New file-parsing capabilities spawn opportunities
RSS
Subscribe to Windows IT Pro | See More Security Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

I've noticed two positive health indicators for the future of command-shell scripting. The first is the number of new third-party tools that I'm seeing on the market, and the second is that many of these tools contain built-in parsing capabilities for input files. Those of us who have mastered the use of the For command to parse input files and call commands might not think these built-in capabilities are crucial, but at least utility developers are finally understanding that we don't always use their tools against one node but often against hundreds or even thousands of nodes.

What other less-obvious advantages might you realize by having input-file parsing built into a tool? I can think of a big one. If you're using Task Scheduler to schedule tasks, you now have another option: Instead of launching a script that calls a tool, you can go to your task's Properties sheet and place commands directly on the Run line. It's possible that even after you stop a scripted task in Task Scheduler, a process that launched with the script might continue working until you discover it and kill it. When you stop a task that's directly specified on the Run line—such as the BeyondExec (beyond-execv2.exe) tool that I talk about in this article—it gets killed. (The only possibility of a run-on process would be one launched on a remote machine that continues until completion.)

I recently stumbled onto seven great command-line tools at Beyond Logic (http://www.beyondlogic.org). You might find all these utilities to be useful additions to your command-shell toolkit, but I want to devote the bulk of this article to BeyondExec, the company's remote process-spawning and shutdown tool. To view the tool's online Help, run the standard command

Beyondexecv2.exe /?

The online Help reveals some cool switch options, as you can see in Table 1, page 12.

Shutdown
To understand this tool's capabilities, let's dive into some sample code to get familiar with its shutdown features. Suppose you have an environment in which you need to lock down workstations at night. But some users have disabled their password screensavers or have increased the screensaver's delay time because the default setting inconvenienced them. (Such actions can expose a PC to a security breach should an unauthorized user happen upon the node and use the logged-in user's security context to access unauthorized data.) You also have a few users who tend to work late to complete special projects. To accomplish your goal of locking all workstations—but to also give users who are still working a 2-minute cancellation option if they're still online—you come up with the following code:

Beyondexecv2.exe
 -g "D:\serverlist.txt"
 -d LockWorkstation
 -m "Locking Workstation Now"
 -l 120

(Be sure you type the entire command on one line.) With the -g switch, you've specified an input list, and with the -d switch you've specified an action parameter—in this case, LockWorkstation. You could choose Hibernate, Logoff, PowerOff, Reboot, Shutdown, or Suspend, so you have much flexibility in specifying what you want to occur on the remote workstation. The -m switch specifies the user message, and the -l switch specifies the length of time (in seconds) to display the message. If the -l switch is omitted, the default delay is 60 seconds.

If you want to prevent users from canceling your action, you can override the default setting of displaying a Cancel button by using the -x switch to shade that option. If a targeted workstation is already in a locked state, the Logout, Reboot, and Shutdown actions won't function correctly unless you also use the -f switch, which forces applications to terminate. However, the Hibernate and Suspend actions will function correctly on a locked workstation.

Have you ever needed to trigger a reboot or shut down a single computer or group of remote computers without having to install a remote client on your target computers? The following command demonstrates how easy it is to do so. This sample command reboots the machines specified in the serverlist.txt file. In this example, the delay time is longer than the default 60 seconds (-l), applications are forced to close (-f), and the user is prevented from canceling the reboot action (-x)

Beyondexecv2.exe
  -g "D:\serverlist.txt"
  -d Reboot
  -m "Shutting down and restarting in 2 minutes. Please save your work and close all applications"
  -l 120 -f -x

If you're planning to use Task Scheduler locally to perform regular reboots or shutdowns on a few nodes, you might take a moment to consider a smaller Beyond Logic tool for this task. Shutdown.exe, the company's small (44KB), single-node shutdown tool, provides a simplified subset of BeyondExec's features. Just be sure to examine the switch options closely because they differ slightly from those of the full-function BeyondExec tool.

Process Spawning
Now, let's look at BeyondExec's process-spawning features. As Microsoft begins to introduce more and more commands that you can execute both locally and remotely, we might find that we don't need these types of process-spawning tools as much, but for now they remain important utilities in our scripting toolkits.

Whenever I talk about launching processes on remote machines, I remember my old favorite utility, Sysinternals' PsExec, which is easily obtainable from http://www.sysinternals.com/ntw2k/freeware/psexec.shtml. I've often used PsExec to launch scripts or utilities on remote machines. You might ask why you need a second tool for the same job if you already have one that works. The reason I like to investigate tools that perform similar functions is that sometimes small nuances can make one tool better for a given task.

PsExec can send commands to a list but also to an entire domain of computers, and it can prioritize remote processes. Unlike PsExec, BeyondExec can send shutdown commands combined with installation commands, making it perfect for service packs and hotfixes that require reboots after completion. Also, in some cases, you might find—as I did—that McAfee antivirus software detects PsExec as a malicious program, whereas McAfee lets BeyondExec work unimpeded. So, for many reasons, you don't want to get too comfortable with just one utility. Always test new options.

Here's a code sample for using BeyondExec to perform a Windows 2000 Service Pack 4 (SP4) installation:

Beyondexecv2.exe
  -g "D:\SPtargetlist.txt" 
  -c -d reboot -f 
  -m "Service Pack 4 has been installed on your PC. Please reboot your computer as soon as possible."
D:\SP4SourceFiles\w2ksp4_en.exe
  -q -n -z

This code uses a combination of BeyondExec switches (-g, -c, -d, -f, and -m) and Microsoft service pack switches (-q, -n, and -z). The -c switch copies the SP4 executable to the target computer. As for the service pack switches, the -q switch runs the installation in quiet mode, the -n switch prevents the backup of files for uninstallation, and the -z switch prevents the system from rebooting following the service pack installation. Instead of allowing the service pack to dictate the reboot, the BeyondExec tool can perform a more graceful reboot by letting you issue an appropriate user message.

This message gives the user time to cancel the reboot if he or she is still using the PC, but communicates the need for a later reboot. Computers that are online but not in use would receive the service pack installation and would reboot. Computers that are offline could receive a Wake-On-LAN (WOL) packet to bring them online before the installation begins. Of course, when you're dealing with a crucial system modification such as a service pack or hotfix installation, you should carefully test the remote-installation tool options and service pack or hotfix switches you plan to use before you roll them out to production systems.

For more information about BeyondExec, see the Beyond Logic Web site at http://www.beyondlogic.org/consulting/remoteprocess/BeyondExec.htm. Be sure to refer to that documentation along with the aforementioned Help before attempting to implement the tool.

Other Cool Tools
In addition to BeyondExec and Shutdown.exe, Beyond Logic offers five more freeware utilities, which I list here for your information. These tools are designed to run locally, and they have few, if any, switch options to configure. I don't have space to give them a lot of coverage, but they're all interesting and deserving of honorable mentions.

  • Bmail.exe is a command-line SMTP mailer. It's small and simple and gives you an alternative to Blat. The tool can handle MIME attachments with the use of a freeware mpack add-on, which is available at Beyond Logic's Web site.
  • Process.exe is a command-line process viewer/killer/suspender. If you've ever had a wayward process that refused to die when you attempted to kill it in Task Manager, this utility can help. Also, you can use Process.exe to view, suspend, or set the priority or affinity of processes.
  • Delbyowner.exe is a delete/copy utility that lets you specify files by owner. If you ever need to archive or clean up old employee files, Delbyowner.exe will help you locate, copy, and delete files that a certain individual owned.
  • Compinfo.exe is a console computer-information utility. If you want a quick view that gives you more details than srvinfo.exe provides, use compinfo.exe to display a PC's hardware specifications, including processor, OS, service-pack, physical and virtual memory, network-address, logical-drive, video-card, hard-disk, CD-ROM, and printer information.
  • Smart.exe permits access to hard disks' Self-Monitoring Analysis and Reporting Technology (SMART) attributes. By running Smart.exe against a SMART-compliant disk, you can get a quick summary of the health of any disk without having to install a program or take the disk offline.

Get Logical
Keeping up with all the newest tools is difficult but can be quite rewarding. In this article, I've reviewed the syntax for the remote-execution and shutdown tool BeyondExec. I've also taken a quick look at several other tools from Beyond Logic. These are certainly tools that you'll want to add to your scripting toolkit. By continuing to address the needs of systems administrators, developers validate the health and popularity of command-shell scripting.

TABLE 1: BeyondExec Switch Options
Switch Action
-u Provide an account name with administrator rights on the remote machine.
-p Provide a password for the above account.
-s Use the System account.
-i Allow the process to interact with the desktop.
-t Terminate the process after the specified number of seconds.
-q Specifiy priority. Options are AboveNormal, BelowNormal, HighPriority, Idle, Normal, and Realtime.
-c Copy the file to a remote computer before executing (default security).
-c sCopy the file to a remote computer before executing (set security).
-w Don't wait for the process to finish; return immediately.
-b Bypass remote driver checks (assumes driver is already running).
-d Take down computer. Options are Hibernate, LockWorkstation, Logoff, PowerOff, Reboot, Shutdown, and Suspend.
-f Force applications to terminate.
-m Display message to user.
-l Display message for the specified number of seconds (default is 60 seconds).
-x Prevent user from canceling shutdown (shaded button).
-n Ignore computers in use.
-g Use multiple computers specified by a group file.
-r Stop and remove the BeyondExec driver on remote computers.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Accessing Database Data with ADO

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...


Security Whitepapers Protecting (You and) Your Data with Exchange Server 2007

Extended Validation SSL Certificates

Unauthorized applications: Taking back control

Related Events Check out our list of Free Email Newsletters!

Security eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

Related Security Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing