I’ve been tinkering with PowerShell in my spare time over the past month – it was one of the technologies that I really wanted to learn after putting together my post on Continuous Education. I see PowerShell as a great “glue” technology. I’ve read just about every document, article and post and now I’m really trying to grok it.
What strikes me about PowerShell is just how simple it is to write useful scripts in a few minutes to do the job of a tool you’d have to write as a console app in C# that would take you hours to craft. A few weeks ago I wanted to write some documentation about a build process - I didn’t fancy trawling though all the files and copying and pasting all the target names and writing descriptions by hand, but I had just recently been investigating how PowerShell deals with parsing XML files.
The following script requires that your MSBuild file has a “.proj” file extension – it will loop though all the “Target” elements and extract the “name” attribute and then look for a single “Message” element and extract the “text” attribute. It will then convert the output to HTML and save it to the location specified in the format {MSBuildFileName}.html
$buildDirectory = "C:\Projects\build"
$documentOutputPath = "C:\"
foreach ($file in get-childitem $buildDirectory)
{
#Only look for .proj and .target files
if ($file.extension.contains(".proj"))
{
$msBuildFile = [xml](get-content $file.FullName)
$outputFile = ($documentOutputPath + ($file.Name.Replace($file.Extension, "")) + ".html")
#Only attempt to parse the file if it has the MSBuild XMLNS
if ($msBuildFile.Project.xmlns -eq "http://schemas.microsoft.com/developer/msbuild/2003")
{
#create expressions to rename the properties to something useful
$target = @{Name = "Target"; expression = { $_.Name }}
$comment = @{Name = "Comment"; expression = { $_.Message.Text }}
#Use the expressions to select the target name and comment and
#output to a html file based on the name of the file
$msBuildFile.Project.Target | select-Object $target, $comment | convertto-Html > $outputFile
}
}
}