Prof. Powershell

Get That Server

PowerShell 2.0 has the power to do more. But you gotta read the help to find out what works and what doesn't. Herewith, an example.

PowerShell 1.0 introduced us to some very handy cmdlets, like Get-Process and Get-Service. Unfortunately, they only worked locally. If you wanted to run Get-Process and filter out the high memory processes on a remote server, you had to go to that server, logon, open PowerShell and run your command. Or figure out how to do it using Get-WMIObject.

Happily, these cmdlets in PowerShell 2.0 now sport a long needed -Computername parameter:

PS C:\> get-service -computername File01

You can now run the Get-Process command from your desktop, like this:


PS C:\> ps -ComputerName JDHIT01 | sort ws -desc | select -first 10

Handles NPM(K)   PM(K)   WS(K) VM(M) CPU(s)   Id ProcessName
------- ------   -----   ----- ----- ------   -- -----------
    388      9    4328  162140   181         472 services
   1172     44   22528   31876   125         936 svchost
    960     86   25580   30540    97         484 lsass
   5199    777   23300   22760    70        1352 dns
    522     48    9944   13996    95        1508 inetinfo
    612     65    9268   13216    64         424 winlogon
    303    331    8464   11532   102        1788 tcpsvcs
    351      8    7836   11464    83        2028 iexplore
    211      8    5340    8092    47        1172 spoolsv
    239     16    6156    7104    89        1716 wins

These parameters do NOT require that PowerShell 2.0 be installed on the remote machine. However you probably still will need it, as you'll run into some limitations. For example, the Stop-Service cmdlet does not have a -computername parameter, so you can't do this, for example:

PS C:\> gsv spooler -computername "jdhit01" | stop-service

Well, technically you can run this without error, as it will stop the spooler service on your machine. If you tried this, it will definitely fail:

PS C:\> gsv spooler -computername "jdhit01" | stop-service -computername "jdhit01"

The Stop-Service cmdlet simply doesn't support it.

The other limitation, before you get too excited, is that cmdlets like Get-Service and Get-Process, while now connecting to remote computers, can't use alternate credentials. The cmdlets will use the current credentials. If you need alternate credential support, you'll have to turn back to Get-WMIObject.

What I really want you to take away from this lesson is that you should re-read the help documentation for cmdlets in PowerShell 2.0 -- even those you have used in the past. You might be surprised by a new parameter or two.


About the Author

Jeffery Hicks (MCSE,MCSA,MCT) is a Microsoft MVP and an IT veteran with almost 20 years of experience, much of it spent as an IT consultant specializing in Windows server technologies. He works today as an independent author, trainer and consultant. Jeff has co-authored or authored several books, courseware, and training videos on administrative scripting and automation. His latest book is Managing Active Directory with Windows PowerShell: TFM (SAPIEN Press 2008). You can follow Jeff at twitter.com/jeffhicks and jdhitsolutions.com/blog.

Reader Comments:

Wed, Nov 4, 2009 iHunger St. Louis, MO

If you are using VMware virtual machines, you have a few more options for remotely killing procs, etc. using PowerCLI with PowerShell. IMO, learning PowerShell will be key to surviving in the post Window 7 world, but IT Admins face a big learning curve for PowerShell. For PowerShell Newbies, I would recommend checking out PowerWF (http://www.powerwf.com). It lets you import PowerShell samples you find on the web, and turns them into Windows Workflows. You can also drag and drop PowerShell activities to create a workflow that can be turned into PowerShell cmdlets, snapins, or modules, or even Win Apps.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above