Creating and Using a Configuration File for Your PowerShell Scripts

.NET has this nice and easy ability to load application configuration information from an XML config file associated with the application.  Unfortunately there isn’t a way to put config info in a config file associated with a DLL e.g. Acme.dll.config.  The only way to get config information from the .NET System.Configuration types is to put your config info in the application’s config file.  For PowerShell scripts this would mean adding the config info to PowerShell.exe.config.  That just seems wrong to me.
 
Considering how well PowerShell supports XML it is pretty easy to consume config information from a similarly formatted config file.  Take this config file as an example:
 

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" safemode="true"/>
    <requiredRuntime version="v2.0.50727" safemode="true"/>
  </startup>
  <appSettings>
    <add key="Editions" value="Standard Edition,Desktop Edition,Desktop Engine,Evaluation Edition,Enterprise Edition,Developer Edition,Personal Edition,Enterprise Edition (64-bit),Developer Edition (64-bit),Enterprise Evaluation Edition,Small Business Server Edition" />
    <add key="Categories" value="Backup and Recovery,Configuration Options,Database Design,Database Administration,Deprecation,Full-Text,General Administration,Generic,T-SQL,SQL Server 2005 Readiness" />
    <add key="DataTypes" value="string,int,double" />
    <add key="Database" value="" />
    <add key="SQLServer" value="" />
    <add key="WindowsAuthentication" value="" />
    <add key="MaxScanDetailRows" value="500" />
    <!– Logging level. 0 – Off, 1 – Error, 2 – Warning, 3 – Info, >=4 – Verbose –>
    <add key="LoggingLevel" value="2" />
  </appSettings>
</configuration>

 
Save the following to a script called LoadConfig.ps1:
 
param($path = $(throw "You must specify a config file"))
$global:appSettings = @{}
$config = [xml](get-content $path)
foreach ($addNode in $config.configuration.appsettings.add) {
 if ($addNode.Value.Contains(‘,’)) {
  # Array case
  $value = $addNode.Value.Split(‘,’)

  for ($i = 0; $i -lt $value.length; $i++) {
    $value[$i] = $value[$i].Trim()
  }
 }
 else {
  # Scalar case
  $value = $addNode.Value
 }
 $global:appSettings[$addNode.Key] = $value
}

 
Now at the top of your scripts you can execute:
 
.\LoadConfig scriptname.config
 
and then access settings like so:
 
$appSettings["MaxScanDetailRows"]
500
 
$appSettings["datatypes"]
string
int
double
 
$appSettings["datatypes"][0]
string 
 

Of course you don’t have to use the same config format that .NET uses.  You could change the XML structure to make it simpler.  Just modify the LoadConfig.ps1 script accordingly.

About these ads
This entry was posted in PowerShell. Bookmark the permalink.

One Response to Creating and Using a Configuration File for Your PowerShell Scripts

  1. Grateful HumanBeing says:

    Nice. +1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s