3 October 2012

Powershell XML Configuration Files.

Recently I have been playing with Powershell and while I still prefer Bash as a shell (long time user) I was impressed with the amount that can be achieved with Powershell.

One of the things that I was looking at doing was automating our build process as we had hit a css file count limit in Internet Explorer (31). I should point out that our css is not that badly laid out but we load specific css files dynamically based on the client's needs. So my aim was to minify our css with the Ajax Minifer tool which can be found on Codeplex. The only issue with our current development set up is that my co-developer hosts his svn working directories on a different drive to mine so I needed to provide a configuration file to specify the path to Ajax Minifer and our code.

Turns our this is really easy in Powershell as it can natively parse XML and provides a nice tree structure to use. The following reads, parses and provides the XML configuration file as said tree :

# read the local config file for various settings ...
$cfgFile = "$PATH_TO_FILE"
$root = "$NAME_OF_ROOT_NODE"
$cfg = [xml](gc $cfgFile)

At this point you have access to the child nodes through $cfg. For example $cfg.$root.EnableDebugging -eq "True" returns the value of the <EnableDebugging> child node and check's whether it's value is set to True.

As with all scripting and shells there appears to be more than one way to achieve this but it works perfectly for what I needed.

No comments: