Posey's Tips & Tricks

Using Configuration Files to Control PowerShell Scripts, Part 3

Learn how to leverage configuration files to customize PowerShell scripts, including language, formatting and display settings, in this conclusion to a three-part series.

In the previous article in this series, I showed you how my Setup.ps1 script works and how it generates a configuration file that can be used by my Hello World script. Now, I want to conclude the series by showing you how my Hello World script works. As before, I am only going to lightly touch on the GUI elements, except for where they are being customized by the configuration file.

The script begins by reading the configuration file and converting it from JSON format into a format that PowerShell can use. Here is the code used to read the file and perform the conversion:

# Path to the configuration file
$ConfigFile = "C:\Scripts\Hello-World-With-Config-File\Config.json"

# Load the configuration file
$Config = Get-Content -Path $configFile | ConvertFrom-Json

The next thing that PowerShell has to do is to extract the values from the configuration file. I am assigning each value to a variable so that I can work with those values more easily. Here is the code that creates the variable that will be used by the script:

$Language = $Config.Language
$FontSize = [int]$Config.FontSize
$FontColor = $Config.FontColor
$BackgroundColor = $Config.BackgroundColor
$Resolution = $Config.Resolution
$DateFormat = $Config.DateFormat

When I created this project I intentionally wrote the script in a way that would allow some of these variables to be used as-is, while others would require special handling. That way, you could get a better feel for how configuration files work in the real world.

In the case of the Language value, the language is English, Spanish or something like that. However, I need for the script to produce a text string that is based on the chosen language. What I did was to set up a Switch statement that assigns a string to a variable called $HelloWorldText, based on the selected language. You can see the code here:

# Set up the translation for "Hello World"  based on language setting
$HelloWorldText = Switch ($Language) {
"English" { "Hello, World!" }
"Spanish" { "¡Hola, Mundo!" }
"Italian" { "Ciao, Mondo!" }
"French" { "Bonjour le Monde!"}
}

So in other words, if the language specified within the configuration file is English, then the $HelloWorldText variable will be set to "Hello World!"

I handled the dates in a similar way. As you will recall, the Setup script asks the user to choose a date format. I have therefore created a switch statement that checks to see which date format was selected, and then assigns the date to a variable called $TodaysDate in the preferred format. You can see the code here:

#Set the date format based on the specified configuration
$TodaysDate = Switch ($DateFormat) {
"Long Format" {(Get-Date).ToString("F")}
"MM-DD-YYYY" {(Get-Date).ToString("MM-dd-yyyy")}  
"Month Year" {(Get-Date).ToString("MMMM yyyy")}
}

As you may recall, the configuration file is also being used to define the size of the window. There are a few different ways that I could have written the code, but ultimately ended up using two separate switch statements to extract the horizontal and vertical resolutions. The switch statements themselves work exactly like those used to handle the language and the date. This time however, I am creating variables called $HorizontalResolution and $VerticalResolution, which will be used to define the window resolution. You can see those switch statements here:

$HorizontalResolution = Switch ($Resolution){
"640 x 480" {[int]640}
"800 x 600" {[int]800}
"1024 x 768" {[int]1024}
}
$VerticalResolution = Switch ($Resolution){
"640 x 480" {[int]480}
"800 x 600" {[int]600}
"1024 x 768" {[int]768}
}

 

The GUI interface is created quite a bit further down in the code. As previously noted, I am not going to delve heavily into the GUI because it’s very similar to what I covered in the last article. However, I do want to show you how the window size is set.

The block of code shown below creates the form. If you look at the last line of code within this block, you can see that rather than hard coding the form (window) size, I am referencing the $HorizontalResolution and $VerticalResolution variables.

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Hello World"
$Form.Size = New-Object System.Drawing.Size($HorizontalResolution,$VerticalResolution)

 

I was able to handle the other configuration items in a much simpler way. For example, I was able to apply the background color directly to the form without doing anything special. Here is the line of code that applies the background color:

$Form.BackColor = [System.Drawing.Color]::$($BackgroundColor)

The Hello World text is displayed using a GUI label called $HelloWorldLabel.  Here is the code used to create that label:

$HelloWorldLabel = New-Object system.Windows.Forms.Label
$HelloWorldLabel.Text = $HelloWorldText
$HelloWorldLabel.Font = New-Object System.Drawing.Font("Arial", $FontSize)
$HelloWorldLabel.ForeColor = [System.Drawing.Color]::$($FontColor)
$HelloWorldLabel.AutoSize = $true
$HelloWorldLabel.Location = New-Object System.Drawing.Point(5,50)

 

As you look at this code, you will notice that I was able to use the font size and the font color defined by the configuration file, without having to do anything special. This is also where the Hello World text comes into play. If you look at the second line in the code block above, you can see that $HelloWorldLabel.Text is being set equal to the $HelloWorldText variable. This variable contains the Hello World message in the chosen language.

The font size and color are also being directly applied to the label used for displaying the date. I am also assigning the $TodaysDate variable, which contains the date in the selected format to $DateLabel.Text. You can see the date label code below:

$DateLabel = New-Object System.Windows.Forms.Label
$DateLabel.Text = $TodaysDate
$DateLabel.Font = New-Object System.Drawing.Font("Arial", $FontSize)
$DateLabel.ForeColor = [System.Drawing.Color]::$($FontColor)
$DateLabel.AutoSize = $true
$DateLabel.Location = New-Object System.Drawing.Point(5,100)

 

So as you can see, all I really had to do was to read the various values from the configuration file and then put those values into a format that PowerShell could use. Once that was done, I was free to use the configuration data however I wanted to. With that said, here is the full source code for my Hello World script:

# Path to the configuration file
$ConfigFile = "C:\Scripts\Hello-World-With-Config-File\Config.json"

# Load the configuration file
$Config = Get-Content -Path $configFile | ConvertFrom-Json

 

# Get the config file values

$Language = $Config.Language
$FontSize = [int]$Config.FontSize
$FontColor = $Config.FontColor
$BackgroundColor = $Config.BackgroundColor
$Resolution = $Config.Resolution
$DateFormat = $Config.DateFormat

 

# Set up the translation for "Hello World" based on language setting
$HelloWorldText = Switch ($Language) {
"English" { "Hello, World!" }
"Spanish" { "¡Hola, Mundo!" }
"Italian" { "Ciao, Mondo!" }
"French" { "Bonjour le Monde!"}
}

#Set the date format based on the specified configuration
$TodaysDate = Switch ($DateFormat) {
"Long Format" {(Get-Date).ToString("F")}
"MM-DD-YYYY" {(Get-Date).ToString("MM-dd-yyyy")}  
"Month Year" {(Get-Date).ToString("MMMM yyyy")}
}

#Acquire the horizontal and vertical screen resolution based on the config file
$HorizontalResolution = Switch ($Resolution){
"640 x 480" {[int]640}
"800 x 600" {[int]800}
"1024 x 768" {[int]1024}
}
$VerticalResolution = Switch ($Resolution){
"640 x 480" {[int]480}
"800 x 600" {[int]600}
"1024 x 768" {[int]768}
}

 

 

# Load necessary assemblies for GUI
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

 

# Create the form

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Hello World"
$Form.Size = New-Object System.Drawing.Size($HorizontalResolution,$VerticalResolution)

# Apply background color
$Form.BackColor = [System.Drawing.Color]::$($BackgroundColor)

# Create the labels

$HelloWorldLabel = New-Object system.Windows.Forms.Label
$HelloWorldLabel.Text = $HelloWorldText
$HelloWorldLabel.Font = New-Object System.Drawing.Font("Arial", $FontSize)
$HelloWorldLabel.ForeColor = [System.Drawing.Color]::$($FontColor)
$HelloWorldLabel.AutoSize = $true
$HelloWorldLabel.Location = New-Object System.Drawing.Point(5,50)

$DateLabel = New-Object System.Windows.Forms.Label
$DateLabel.Text = $TodaysDate
$DateLabel.Font = New-Object System.Drawing.Font("Arial", $FontSize)
$DateLabel.ForeColor = [System.Drawing.Color]::$($FontColor)
$DateLabel.AutoSize = $true
$DateLabel.Location = New-Object System.Drawing.Point(5,100)

# Add the label to the form
$Form.Controls.Add($HelloWorldLabel)
$Form.Controls.Add($DateLabel)

# Show the form
$form.ShowDialog()

 

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

comments powered by Disqus

Subscribe on YouTube