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


April 2009

Use PowerShell to Execute Commands on Remote Machines

An alternative to using PsExec
RSS
Subscribe to Windows IT Pro | See More Tips Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

I wanted to run the ipconfig /all command on all my remote servers and have the results returned to my local machine. I knew that I could use PsExec from the PsTools suite, but I wanted to see how well PowerShell 1.0 worked for this task. (In PowerShell 2.0, you can run PowerShell commands directly against remote computers, but this version is still in the Community Technology Preview—CTP—stage.) After some investigation and trial runs, I created the RemoteProcess.ps1 script in Listing 1.

Listing 1: RemoteProcess.ps1

As callout A in Listing 1 shows, you can use Windows Management Instrumentation's (WMI's) Win32_Process class to run a remote process in PowerShell 1.0. Specifically, I use the [wmiclass] type accelerator to connect to WMI's ROOT\CIMV2 namespace and access the Win32_Process instance on the computer that's specified in the $s variable. (For information about the WMI type accelerator, see "Type Accelerators: A Useful But Undocumented Feature in PowerShell 1.0.") I then use that class's Create method to start a remote process that runs the ipconfig /all command. The command's results are stored in a text file on the remote server's C drive. This output file's name follows the format $s-ipconfig.txt, where $s is the remote server's name.

Besides storing the ipconfig command's output, I capture the return value of the Win32_Process class's Create method. This method returns a value of 0 when the process is successfully created; any other number indicates an error. The different values are shown in the switch statement in callout B in Listing 1. (You can find more information about them in MSDN's Create Method of the Win32_Process Class web page.) When the Create method returns a value, this switch statement displays the remote server's name followed by the appropriate message based on that value. For example, if the Create method returns a value of 2, the switch statement displays the message Access Denied after the remote server's name.

When the Create method is successful, the ipconfig /all command runs and the results are saved on the remote server's C drive. I didn't want to keep the results on each remote machine, so I used the Move-Item cmdlet in the statement

Move-Item -path \\$s\c$\$s-ipconfig.txt `
  $destination -force 

to move them to my local machine. However, when I ran the script multiple times to test it, I received an error message along the lines of Move-Item: Cannot find path '\\mycomputer\c$\mycomputer-ipconfig.txt' because it does not exist. Only then did I realize that it takes time to run a Win32 process on remote machine that's across a network—and PowerShell doesn't wait for that process to terminate before it starts executing the next command. So, when PowerShell started executing the Move-Item cmdlet statement, it couldn't find the file because the file didn't exist yet.

To overcome this problem, I used the Microsoft .NET Framework System.Diagnostics.Process class's Start method to execute the Move-Item cmdlet because PowerShell then has to wait for the Win32 process to finish before continuing to the next command. As the code in callout C in Listing 1 shows, I also used the System.Diagnostics.Process class's WaitForExit method to set a maximum wait time. That way, PowerShell won't continue until either the remote process finishes or 3 seconds have elapsed. (For more information about the Start and WaitForExit methods, see the web-exclusive article "Q. When I run a Win32 process from Windows PowerShell, how can I pause PowerShell so that the process can exit before I run other PowerShell commands?")

You can download RemoteProcess.ps1 by clicking the Download the Code Here button at the top of the page. Before you use RemoteProcess.ps1, you need to customize it and create an input file.

To customize RemoteProcess.ps1, open it in a text editor, such as Notepad. In the first line

$destination = \\mycomputer\d$\ipconfig 

change \\mycomputer\d$\ipconfig to the location where you want to store the output files. In the second line

$servers = Get-Content D:\servers.txt 

change D:\servers.txt to your input file's pathname.

To create the input file, construct a text file that contains the names of the remote servers on which you want to run the ipconfig /all command. Each server name needs to be on a separate line.

I wrote this script for remote servers running Windows Server 2003. You need to run PowerShell as an administrator for this script to work because there's no way to provide alternative credentials when using the [WMI] type accelerator.

End of Article



Reader Comments

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
Command Prompt Tricks

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

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. ...


Related Articles 4 Ways to Help PowerShell Find External Tools

Test for Numerous Conditions with PowerShell's switch Statement

Controlling Your Code's Flow with PowerShell's Conditional Statements

Iterating Through Collections with PowerShell's foreach Loops

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

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

7 Ways To Get More From Your SharePoint Deployment Now

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