Reading Configuration Files with Powershell Part 2

Following the recently published article on reading xml configuration files using PowerShell, we are going to enrich the configuration file and create custom PowerShell objects.

This time the configuration file is a bit different, we have multiple "Company" nodes that have inline properties.

    <Company Name="Company1">
        <Group DN="CN=Group1,OU=Groups,OU=Company1,DC=lab,DC=local" Name="Group1"/>
        <OU DN="OU=Users,OU=Company1,DC=lab,DC=local"/>
    <Company Name="Company2">
        <Group DN="CN=Group2,OU=Groups,OU=Company2,DC=lab,DC=local" Name="Group2"/>
        <OU DN="OU=Users,OU=Company2,DC=lab,DC=local"/>

There aren't many changes in the script, just in the variable that holds the entire configuration.


# Test if configuration file exists
if( (Test-Path $ConfigurationPath) -ne $true)
    $message = "The configuration file was not found (" + $ConfigurationPath + ")"
    throw $message

# Read the configuration
    [xml]$Configuration = Get-Content -Path $ConfigurationPath
    $Companies = $Configuration.configuration.Company
    $message = "Error reading configuration file (" + $ConfigurationPath + ")"
    throw $message

When the configuration is red from the file, the "Companies" variable will be holding all the information. This is going to be an array of objects since we have multiple "Company" nodes.

Each object in the array will have the "Name" property, and two custom properties named "Group" and "OU". The group custom property will have two properties named "DN" and "Name" and the "OU" custom property will only have "DN".

PS C:\> $Companies[0]
Name Group OU
---- ----- --
Company1 Group OU

PS C:\> $Companies[0].Group
DN Name
-- ----
CN=Group1,OU=Groups,OU=Company1,DC=lab,DC=local Group1

PS C:\> $Companies[0].OU

PS C:\>  

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS