PS – configuration for bigger scripts and simple logging

Hello all

In this post i want to show you how to create a configuration with a XML-file easily.
Lots of configuration parameters can be placed in here so that the end user makes his changes in this XML-file where the impact of modifications can be mitigated. Also it cleans up the source code. By loading all relevant parameters within one file you have to look and store all properties at only one place.

Also you can place complex configuration lists and conditional matrixes in it to load them later on with powershell and filter the outcome.

Some examples:

Loading the XML-file

[xml] $configXml = Get-Content -Path "$PSScriptRoot\Config.xml" -ErrorAction 'Stop'

Yeah – that easy.

Exemplaric structure of a file:

<?xml version="1.0" standalone="yes"?>
<Config>
   <DBConnectionString>Provider = OraOLEDB.Oracle; Data Source = ds ; User Id = USER ; Password = pwuser; OLEDB.NET = True;</DBConnectionString>
   <LogDir>c:\Logs</LogDir>
   <ConfigValue Value1="val1"></ConfigValue>
   <ConditionalMatrix>
     <Parameter Value1="val1" Value2="start" BoolValue="1"></Parameter>
     <Parameter Value1="val1" Value2="end" BoolValue="0"></Parameter>
     <Parameter Value1="val2" Value2="test" BoolValue="1"></Parameter>
     <Parameter Value1="val2" Value2="test" BoolValue="0"></Parameter>
     <Parameter Value1="val3" Value2="prod" BoolValue="1"></Parameter>
     <Parameter Value1="val3" Value2="test" BoolValue="0"></Parameter>
   </ConditionalMatrix>
</Config>

Retrieving the data:

#fetching the data directly
$ConnectionString= $configXML.Config.DBConnectionString
$Value1= $configXML.Config.ConfigValue.Value1

$Value1ByConditionalMatrix= ($configXML.Config.ConditionalMatrix.Parameter  | Where-Object {$_.Value2-eq "test" -and $_.BoolValue-eq "1"}).Value1

Easy isnĀ“t it?! Now you have to load the configuration within the script from a relativ path. This could also be the path where afterwards the loggings could be stored into.

A very simple way for logging is to set gather the logging path from config/relative and write log files with a date in it. So you get an overview for each day.

$LogDir=$configXML.Config.LogDir
$LogFile = "$LogDir\Log" + ((Get-Date).Day) + "_" + ((Get-Date).Month) + "_" + ((Get-Date).Year) + "_" + ((Get-Date).Hour) + "_" + ((Get-Date).Minute) +".log"
(Get-Date).ToString()  + " Something happened $Object" >> $LogFile

This can also be empowered even more if you build a separate function for this. So you can grab executing function, date, user etc. which will be written to a log file.

I hope i could give you some hints of the power with working xml-configuration files and enabling you the view for very simple logging.

~David

PS – loading powershell cmdlets for SCCM

If you have SCCM in use you should also throw an eye into the specific powershell cmdlets.
To use them you must previously load them with “Import-Module”. Use also a dynamic environment variable to do this so you can copy it easily from one computer to another without resetting the specific path.

Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)

Exemplaric function:

Get-CMDevice -CollectionName "Win 10 Desktops"

There are up to now a nearly uncountable bunch of cmdlets. You can do nearly everything with these powershell cmdlets in powershell. By automizing some processes you will reduce the errors by human inputs and also increase the speed of this processes.

~David