Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


November 14, 2005

Another Way to Run Programs

RunProgram.vbs uses WMI to execute programs locally or remotely
RSS
View this exclusive article with VIP access -- click here to join |
See More VBScript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!
SideBar    Embedding Double Quotes in the Command

Executing programs on a remote machine is a common administrative task, and various tools are available to perform the job. Sysinternals' PsExec tool is a powerful example. (For more information, see "PsExec," Windows IT Pro, July 2004, InstantDoc ID 42919.) I also created a script, JTRun.vbs, that uses Microsoft's jt.exe and the Task Scheduler service. (For more information, see "Command-Line Task Scheduler," March 2005, InstantDoc ID 45148.)

With some restrictions, Windows Management Instrumentation (WMI) can also run programs locally or on a remote computer. Table 1 shows some of the advantages and disadvantages of each approach. In this article, I'll present a script that uses WMI to run programs either locally or remotely. It can also optionally wait for the program to end and display its run time.

The RunProgram.vbs Command
RunProgram.vbs uses WMI's Win32_ProcessStartup and Win32_Process objects to run a program either locally or remotely. You can download the script by going to http://www.windowsitpro.com/windowsscripting, entering 48218 in the InstantDoc ID text box, and clicking the 48218.zip hotlink. RunProgram.vbs requires Windows 2000 with Windows Script Host (WSH) 5.6 or Windows XP or later. (WSH 5.6 is preinstalled on XP and later.) The syntax for the launch command is

RunProgram.vbs "command"
  [/startin:path]
  [/window:n]
  [/computer:computer
  [/username:username]
  [/password:password]]
  [/wait [/elapsed]]

The command argument specifies the command line to execute. Make sure to enclose it in double quotes (""). The command name at the beginning of the command line must be an executable, so if you need to run a cmd.exe command (e.g., Dir, Copy) or a cmd.exe shell script, start the command with cmd /c. If you need to run a WSH script, start the command with cscript or wscript. . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
Reader Comments
Grate

adelino.lopes@ficsa.com.br March 13, 2006 (Article Rating: )


asdas

rblackk December 31, 2006 (Article Rating: )


this doesnt work

zrer10 June 20, 2008 (Article Rating: )


zrer10: Thanks for your comment. Can you please provide more details on the problem you’re having? If I have more information, I can contact the author to see if he can solve the problem.

AnneG_editor July 09, 2008 (Article Rating: )


It's easy to run a remote script now. But i want to check the exit code of the remote vbs script. How is this possible?

gjschipper75 August 21, 2008 (Article Rating: )


Hi gjschipper75,

That is a great question. Unfortunately, there's no way that I can tell of retrieving a the exit code of a process created with the WMI Win32_Process Create method. To work around this, you could create a shell script (.cmd file) that writes the program's exit code to a file.

Regards,

Bill Stewart

AbqBill August 21, 2008 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Articles How to Multi-Thread VBScript Scripts

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement