Reading Configuration Files with Powershell Part 1
Although there are many tools that are very helpful when it comes to automating tasks, a simple script in most cases may be the fastest and most simple solution. Instead of hardcoding the configuration options for the script within it, wouldn't it be more clean and elegant if you used a configuration file?
As an example I'm going to use the simple task of adding users to a group in Active Directory. The purpose of the script is to add users from a specific OU to a specific distribution group after applying filtering based on the SamAccountName attribute.
Since we want to be able to use the same script for different OU and group combination we have to create some kind of configuration file and what would be better that xml!
With the following xml we are specifying the name of the company, the distinguished name of the group and the OU and two exclusions.
When the script executes, we start by testing if the file exists using the relevant parameter and then we read it to an xml object. This allows us to access the nodes of the xml and save the values that we want to variables in order to use them later on.
If we now execute the script and provide the path to the configuration file as the parameter, the variables "Company", "GroupDN", "OU" and "SamExclusions" are going to be set to the values of the xml file and will be ready to be used.
I won't go into the details of the rest of the script since this is only an example. On the next article of the series we are going to be using more complicated xml files.
As an example I'm going to use the simple task of adding users to a group in Active Directory. The purpose of the script is to add users from a specific OU to a specific distribution group after applying filtering based on the SamAccountName attribute.
Since we want to be able to use the same script for different OU and group combination we have to create some kind of configuration file and what would be better that xml!
With the following xml we are specifying the name of the company, the distinguished name of the group and the OU and two exclusions.
<configuration> <Company> Company1 </Company> <GroupDN> CN=Group,OU=Groups,OU=Company1,DC=lab,DC=local </GroupDN> <OU> OU=Users,OU=Company1,DC=lab,DC=local </OU> <Exclusions> <SamAccountName> User1;User2 </SamAccountName> </Exclusions> </configuration>
When the script executes, we start by testing if the file exists using the relevant parameter and then we read it to an xml object. This allows us to access the nodes of the xml and save the values that we want to variables in order to use them later on.
Param ( [string]$ConfigurationPath ) # Test if configuration file exists if( (Test-Path $ConfigurationPath) -ne $true) { $message = "The configuration file was not found (" + $ConfigurationPath + ")" throw $message } # Read the configuration try { [xml]$Configuration = Get-Content -Path $ConfigurationPath $Company = $Configuration.configuration.Company.Trim() $GroupDN = $Configuration.configuration.GroupDN.Trim() $OU = $Configuration.configuration.OU.Trim() $SamExclusions = $Configuration.configuration.Exclusions.SaMAccountName.Trim().Split(';') } catch { $message = "Error reading configuration file (" + $ConfigurationPath + ")" throw $message }
If we now execute the script and provide the path to the configuration file as the parameter, the variables "Company", "GroupDN", "OU" and "SamExclusions" are going to be set to the values of the xml file and will be ready to be used.
I won't go into the details of the rest of the script since this is only an example. On the next article of the series we are going to be using more complicated xml files.