Jakew
Consulting, hacking, and motorcycles

Getting started with PowerShell

Thursday, 13 November 2008 09:22 by jakew

Over the past few weeks I’ve been using Powershell as my command line. I’ve got a long way to go, but the little bit that I have learned makes me pretty excited. Having a shell that works with .NET objects instead of processing text is pretty cool.

What is really useful in my opinion is the ability to access features of the .NET framework from with in a script. For instance, right now I’m working on setting up scripts to deploy my projects to their development servers so we can do integration testing. One of the projects (solutions) has a service that needs to be stopped while we copy the new files in and then restarted when we are done. You can do it with a batch file easily enough, and power shell can do it too:

Get-WmiObject -computername serverName Win32_Service -Filter "Name='serviceName'").StopService()

Stops the service and:

Get-WmiObject -computername serverName Win32_Service -Filter "Name='serviceName'").StartService()

Starts the service. Inside my script I can get the service’s state with this:

Get-WmiObject -computername serverName Win32_Service -Filter "Name='serviceName'").State

Currently, I don’t really check the services state. I just stop it, copy the files and then restart it. If I was smart I’d check the service’s state to make sure it restarts and send out an alert if it fails to start.

I’ve barely even scratched the surface of what powershell can do. Hopefully another few weeks of work will get me comfortable enough so that I can really go mad with automating stuff.

Also – not well publicized but there is a decent getting started guide included with PowerShell. Look in system32\windowspowershell. There are also a number of examples to look at.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed