Powershell Image Conversion script – Part 3: Configuration functionality


Because I’m one of the type of people that is lazy as hell as it comes to IT, I created functionality that enabled me to create and read a configuration file for the script.

The first part of the article will explain the proces of creating a configuration file. The second part will show how the script works wih reading the configuration file.

In case of configuration files you have a lot of choices what type of file you use. There is a simple text file with the settings separated by comma of a new line . You can choose to use a Microsoft Access DB of any other database type but that will make you rely on a third-party product.
The reason why I chose XML is because it a good standard for setting op data. There are a lot of articles around that show you how to create and read config file. Further I like XML because it just look real clean.

Creating the configuration file

Like I mentioned before  I don’t like to do things by hand too much and I wanted other people to be able to implement the script in a way that you don’t have to be a rocket scientist to set it up.
With the use of input boxes  you have an easy way to let users input values that we can use to create the configuration file.

Powershell doesn’t support input boxes but Visual Basic does. The following code show the function:

# Function Show-Inputbox
# This function generates a inputbox where values can be inserted
# Parameters:
# [string] $question: Question in screen
# [string] $title: Title of the input box
# [string] $default: Default value if the cancel button is clicked
Function Show-Inputbox([string]$question, [string]$title, [string]$default)
[reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
[microsoft.visualbasic.interaction]::InputBox($question, $title, $default)

The reason I created this function is because the function that will create the configuration file will use this functionality multiple times.

The following code will set up the part where the settings are picked up from the user:

# Function Create-Config
# This function makes it possible to create a coniguration file
# Parameters:
# [array] $loc: Full path where the configuration will be created
Function Create-Config($loc)
# Get the settings
$var1 = Show-Inputbox "Source directory images" "Source directory Images" ""
$var2 = Show-Inputbox "Source directory for reorganisation" "Source directory Reorganisation" ""
$var3 = Show-Inputbox "Target directory for images" "Target directory images" ""
$var4 = Show-Inputbox "Target directory for thumbnails" "Target directory thumbnails" ""
$var5 = Show-Inputbox "Target directory for reorganisation" "Target directory Reorganisation" ""
$var6 = Show-Inputbox "Archive directory for images" "Archive directory" ""
$var7 = Show-Inputbox "Size for images" "Size images" ""
$var8 = Show-Inputbox "Size for thumbnails" "Size thumbnails" ""
$var9 = Show-Inputbox "Source extension" "Source extension" ""
$var10 = Show-Inputbox "Target extension" "Target Extension" ""
$var11 = Show-Inputbox "Directory for logfiles" "Log directory" ""
$var12 = Show-Inputbox "Dierctory to clean up" "Directory for cleaning" ""

As you can see the function for the input box is called 12 times to get all the settings.

The next part will show how the XML file is set up and saved:

# Setup XML data
$xmlconfig = [xml]"

# Save the data
If(!(Test-Path $loc))
New-Item $loc -type file

The function can be used in the following way:

Create-Config "E:\Imageconversion\MyConfiguration.xml"

Reading the configuration file

Now we have a way to reate a configuration file. But how do we read the data from it. This can be done with the following code:

$xmlconfig = [xml]( Get-Content $config)
$source = $xmlconfig.Configuration.SourceImages

$sourceorg = @()
Foreach($s In $xmlconfig.Configuration.SourceReorganisation.Source)
$sourceorg = $sourceorg + $s

$destimage = $xmlconfig.Configuration.DestinationImage
$destthumb = $xmlconfig.Configuration.DestinationThumb

$destorg = @()
Foreach($d In $xmlconfig.Configuration.DestinationReorganisation.Destination)
$destorg = $destorg + $d

$archive = $xmlconfig.Configuration.ImageArchive
$sizeimage = $xmlconfig.Configuration.SizeImage
$sizethumb = $xmlconfig.Configuration.SizeThumb
$sourceext = $xmlconfig.Configuration.SourceExtension
$targetext = $xmlconfig.Configuration.TargetExtension
$logdir = $xmlconfig.Configuration.Logdirectory

$cleanfolder = @()
Foreach($c In $xmlconfig.Configuration.CleanFolder.Folder)
$cleanfolder = $cleanfolder + $c

As you can see some variables in the script can handle multiple directories. In the Create-Config function I didn’t implement the feature to add multiple values because in my case that wasn’t an issue.
The above code uses the global variables created in the beginning of our first script by creating parameters for Powershell.

I didn’t make a function for reading the configuration file because later on it’ll only be used once during the execution of the script.

Leave a Reply

Your email address will not be published.