Configuration Files

Personally I believe it is important to put configuration settings in a separate file to your script. So, this page shows you how this can be easily done. First though let me say there are other options with this, so you could write a short "script" that literally just defines constants which is your "configuration", however this "feels" wrong to me. I believe you can use a technique involving has tables and also PowerShell 4 has something new but what follows is a nice simple XML based solution. So, firstly the XML file:

<?xml version="1.0" encoding="utf-8"?>
  <!-- Valid Environments: Dev, Pre-Prod, Prod -->
  <!-- Anything other than the word True will not give debug mode -->
    <Description>The Great Development Environment</Description>
    <Description>Pre-Production Environment</Description>
    <Description>Production Environment</Description>

Next we need a PowerShell script file to process this, here is an example:
Write-Host "Geoff's Config Demo"
[xml]$ConfigFile = Get-Content "Config.xml"
Write-Host "Available Environments:"
ForEach ($Env in $ConfigFile.Settings.Environments.Env) {
    Write-Host " Env: $($Env)"
$CurrEnv = $ConfigFile.Settings.CurrentEnvironment
Write-Host "Current Environment: $($CurrEnv)"
$Desc = $ConfigFile.Settings.($CurrEnv).Description
Write-Host "Description: $($Desc)"
$SDir = $ConfigFile.Settings.($CurrEnv).ScriptDirectory
Write-Host "Script Directory: $($SDir)"
Set-Variable -Option Constant DEBUG $ConfigFile.Settings.Debug.Equals("True", "CurrentCultureIgnoreCase")
if ($DEBUG) { Write-Host "PowerShell script running in DEBUG mode" }

I believe with the PowerShell and XML this little example is self explanatory, so I hope you follow it!