If you’re a Microsoft system administrator, it’s hard to avoid PowerShell. Pretty much any of the recent versions of Microsoft server technologies are heavily reliant upon this scripting system. I’ve been resistent to it for a while now, insisting upon using vbscript for most of my scripting tasks, but I’ve recently done a complete turnaround and embraced it wholeheartedly.
Apologies in advance for a somewhat random blog post – future posts will be more targeted about specific PowerShell topics, but for now, I’m just capturing a few things I’ve learned so far (and trying out the Syntax Highlighter For WordPress Plugin that I just installed).
The right tools for the job
PowerShell scripts can be created or edited in any text editor (for example, Notepad), but for true PowerShell, um, power, you need something more robust. My text editor of choice is EditPlus, which can be a decent working environment for PowerShell, although it lacks syntax highlighting for PowerShell (since it’s not PS-aware). However, I’ve completely fallen in love with Idera’s Powershell Plus IDE. It’s like Visual Studio for PowerShell. It’s not cheap ($199/user), but if you’re going to be a hardcore PowerShell scripter, you’ll really see the value. I’m planning on doing a deeper dive on this tool in a future blog post (someone make sure to remind me that I promised to do this!)
Some example code
My current project is to use PowerShell to automate the configuration of newly provisioned IIS servers in our dev/test environment. All of our test servers require a substantial amount (over a dozen) of IIS sites per box, and they all have pretty specific configurations. Doing this manually is a real pain the butt, as well as running the risk of inconsistent configuration (for example, my senior sysadmin is always complaining about the fact that I never remember to change the log file directory for IIS sites I build. This is why it’s dangerous to allow management to actually do technical work, apparently.)
First things first – if you are using PowerShell 2.0, and want to do any cool IIS manipulation, you need to load the WebAdministration module, as follows:
[powershell]
Import-Module WebAdministration
[/powershell]
Once I’ve done this, I can do some nifty things. For example, we never use “Default Web Site”. You can’t remove this site with the GUI, but PowerShell lets you whack it completely:
[powershell]
if (Test-Path “iis:sites\Default Web Site”){
Remove-Website -name “Default Web Site”
}
[/powershell]
See what I did there? First we check if there is a site called “Default Web Site”. If there is, we remove it. Pretty simple.
The other nifty thing to notice is that we can access IIS sites just like they were any other part of the file system (which lets us use the “Test-Path” command).
Once I’ve completed my build script, I’m going to create a post that explains it in detail, but this should illustrate some of the really quick and dirty things you can do with PowerShell that are way harder to do in the GUI.
What about you? What tasks have you started using PowerShell to automate? What are your favorite PowerShell tools or websites?