Go to content Go to navigation Go to search

Brokenwire.NET::Programming

First steps in Windows PowerShell
· 2008-02-24 16:30 by Thijs Kroesbergen for Brokenwire.NET

Last week I made my first moves with Windows PowerShell. I used PowerShell to write a deployment script for the project I'm currently working on. The script is started by TFSDeployer, a great tool to integrate (and automate) deployment with Team Foundation Server.

I started with downloading & installing PowerShell+, a cool wrapper around the regular PowerShell interface. It has many cool features like Intellisense-alike tab-expansion and a tool window where you can inspect all variables that are currently in scope. The best thing about it: It's free for non-commercial use.

Then I continued with the deployment script. I wanted to install some MSI's and the most straightforward solution seemed to use msiexe.exe. Then I did my first steps into the nice world of PowerShell. Below you can find several "bumps" I ran into.

Variables

The usage of variables is easy. Just prefix the variable name with a dollar sign.

Examples:

$noot = "pinda" $sum = 1 + 2 $msifile = "C:\Temp\SuperApp.msi"

String Concatenation:

Easy, just use the + sign, BUT you shouldn't use concatenation on the same line where you execute the external tool. Instead it's better to prepare the entire parameter in a variable and just pass that along.

Example:

$installdir = "C:\install\" $package = $installdir + "MyApp.msi" msiexec /i $package

Running external commands in-process

When starting MsiExec from inside a script the default behaviour is to return control to the script immediately after the background installer service takes control. You can prevent this by piping the output of MsiExec to the out-null cmdlet.

Example:

msiexec /i $msifile | out-null

Visual Studio Command Prompt

After fighting with PowerShell for I while I decided that I wanted to replace my old "command prompt" addiction completely. And because of that I wanted the "VsVars32" aka "Visual Studio Command Prompt" environment variables inside my PowerShell session. Somebody else already figured out how to use vsvars32.bat inside PowerShell, so I used these pieces of code:

This first function executes the batch file and copies the environment variables to PowerShell

function Get-Batchfile ($file) { $cmd = "`"$file`" & set" cmd /c $cmd | Foreach-Object { $p, $v = $_.split('=') Set-Item -path env:$p -value $v } }

The second function retrieves the location of the vsvars32.bat file

function VsVars32($version = "8.0") { $key = "HKLM:SOFTWARE\Microsoft\VisualStudio\" + $version $VsKey = get-ItemProperty $key $VsInstallPath = [System.IO.Path]::GetDirectoryName($VsKey.InstallDir) $VsToolsDir = [System.IO.Path]::GetDirectoryName($VsInstallPath) $VsToolsDir = [System.IO.Path]::Combine($VsToolsDir, "Tools") $BatchFile = [System.IO.Path]::Combine($VsToolsDir, "vsvars32.bat") Get-Batchfile $BatchFile [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell" }

And now, by just running the VsVars function without parameters you have your good old Visual Studio Command prompt set up in PowerShell!

Including other scripts

As soon as I started playing with functions, I realized that I wanted to be able to include external scripts files. I was looking for a "using" (C# style) or "#include" (C++) or maybe even "<!--#include virtual="somefilename"-->" (Classic ASP).

In PowerShell this kind of behavior is called "dot sourcing" and that name says it all. The syntax you use:

. ./somefile.ps1

Easy isn't it? (That is, if you've figured it out once). Look here for a complete overview of the PowerShell syntax, take a look here and here for an overview of the built-in commands.

Discovering commands by understanding the default verbs

All powershell commands (called cmdlets) start with a verb, followed with a dash and noun. You can find a complete verb-list on MDSN but the most important ones I've encountered are:

By trying a verb and pressing the "Tab" key for keyword expansion (with a nice dropdown in PowerShell+) it's easy to "discover" new commands.

Using .NET framework classes in PowerShell

Part of the Power in PowerShell is that you can use .NET framework classes directly in your scripts!

Examples:

$niceandround = [math]::round(45.987654321, 2) $date = [DateTime]::ParseExact("20060908","yyyyMMdd", System.Globalization.CultureInfo]::InvariantCulture)

And because our managers like excel, I'll show you how to do excel too...

$a = New-Object -comobject Excel.Application $a.Visible = $True $b = $a.Workbooks.Add() $c = $b.Worksheets.Item(1) $c.Cells.Item(1,1) = "A value in cell A1." $b.SaveAs("C:\SuperImportantReports\LotsOfProfit.xls") $a.Quit()

Conclusion

That's all for now folks! I hope you'll get started with PowerShell soon if you haven't done so before. If you're new to all this then I promise you'll get excited after you've tried it yourself. Take a look at these 10 tips and you'll be on your way.

 

Ps. It's very probable that I made a mistake in one of the examples above, if you spot one please leave me a comment so I can fix it ;)...

Permalink -

  1. Great post about PowerShell. Thanks.


    Leon Meijer    2008-02-26 09:51    #