During one of our projects, we needed to automate the deployment of the middleware layer. The reason for this was the high frequency of software updates, which sometimes meant we had to rebuild the middleware software layer. We used VMWare vCloud Automation Center to deploy multiple VMs in a short time frame, but we still needed to configure the different Microsoft Server roles and third-party software installations. To prevent errors during script execution, we created some rules on things like network connectivity to other servers or installed components checks. With this DevOps workflow, we created an automation flow that can deploy 120 VMs in just a couple of hours. This normally takes 4 hours per VM when we do the installation manually!
In this blog post, I will just mention a couple of items and will get into the details of our script in later blog posts. The script can be divided into different parts. One of these parts involves logging the steps, as you want to know which steps have been successful (or not) and what time this happened. To start logging, you need to pipe the results into a file.
Logging
Let’s look at the example of writing text output to a file. To do this, we used the “Out-File” command with a variable referring to a file path location. To prevent the results from overwriting the previous log, we used the variable “-Append”. We also piped a timestamp to the command: to create a record, we used a filter function to create the timestamp and to ensure the timestamp was put in front of the record, we used the “ $_” variable. “S_” is the output variable of the placeholder in that command.
The command looks like this:
The result looks like this:
10/3/2018 12:59:34 PM: Start Powershell Installation Script
Checking external connections
Sometimes we need to check whether the network connection has been established before the application can be installed. We can do that using the following Powershell command: Test-NetConnection. To ensure that both positive and negative results are logged when we execute this command, we use an If-Else statement.
The complete Powershell command can be found below:
The result looks like this when no external connection is available:
10/3/2018 12:59:34 PM: Start Powershell Installation Script WARNING: TCP connect to (37.17.209.115 : 80) failed WARNING: Ping to 37.17.209.115 failed with status: TimedOut 10/3/2018 1:00:11 PM: Network connection failed, please check the Network connection on port 80 10/3/2018 1:00:11 PM: Script halted
When successful the result looks like this:
10/3/2018 1:17:13 PM: Start PowerShell Installation Script 10/3/2018 1:17:14 PM: Network connection successfull
Installing software packages
When you want to install software packages, all you need to do is execute the command, but if you want to check first whether the software has already been installed, you will need to create a rule. A simple If-Else statement rule lets you create exceptions for the installation.
To get the status of an installed software package, we used a function to retrieve information from a registry key folder: “HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*”
And to narrow the search results to only “DisplayName”, we used a “Select-Object” command. The filtered results let us specify what we want to do with the If-Else statement. The condition ([IntPtr]::Size -eq 4) used after the If statement means that we are checking we are working with the x86 Powershell platform. For conditions on the x64 platform, we use “8” instead of “4”
These PowerShell commands enable us to automate certain parts of VM configuration and software installations and make our life easier. In the next blog post, I will go into further detail about the other features we used in our PowerShell script.
I would like to thank Michael van Schaik and Varughese Philipose for their involvement in this project. They edited, fine-tuned and tested the scripts.