As we all try to do more with less with automation, we sometimes come to a point where we have a new problem. We are now running so many scripts and processes that in the end we are not sure what "done" looks like
When we run a user registration script that creates an AD (Active Directory) user, Exchange mailbox or a home folder, how do we really know that the script did everything it was supposed to do? We do not. We can see that no error was generated and also check things afterwards. But if the script involves 1
What is infrastructure testing?
One way to automate this type of testing is with what is loosely called infrastructure testing. What is infrastructure testing? It means any code that reads configuration values from different things in the IT environment and compares them to expected values.
Some examples of infrastructure tests are:
- "Was that AD user created with the correct attributes?"
- "Is port 80 responding to my web server?"
- "Is the DHCP service running?"
The discussion about infrastructure testing is much bigger than what we have time for in a single article, but I can give you an edge. When you start writing these tests, you will notice patterns; you will realize that you are repeatedly writing the same kind of code, only applied to different things. Instead of copying and pasting all the time, you can share this code in the form of a PowerShell script or function.
Let's go through some common examples of scripts that I use to help ensure that my infrastructure is configured correctly.  Testing a network port
A typical example of an infrastructure test is testing a network port. Since all network services open and listen on a particular port, a real test is whether that service is 'up'. or not, to confirm that a specific port is available for remote computers. However, building this test requires quite a bit of knowledge about TCP, UDP and .NET. Fortunately, you can use an existing script from the community called Test-NetworkPort.ps1 . This script can be downloaded from the PowerShell Gallery by running
Install-Script -Name Test-NetworkPort .
Once downloaded, simply name it with Test-NetworkPort.ps1 and pass it through a computer name and port for testing.
PS> Test-NetworkPort.ps1 -ComputerName DC -Port 389 Right
Depending on whether the port is listening or not, this script returns
False . You can explore other parameters this script has by reading the associated help from
Get-Help Test-NetworkPort.ps1 .
This script performs a test on the infrastructure listening for a particular port.
Test DNS name resolution
When you start a new machine, whether you rely on dynamic DNS to register the name or if you explicitly create a DNS record, you must ensure that the name can be resolved . With PowerShell, you can use a script that tries to resolve a given name and return
False if it can be resolved or not.
As with our port testing example, we can also download this script from the PowerShell Gallery at
Install-Script -Name Test-DnsNameResolution .
When the download is complete, just call Test-DnsNameResolution.ps1 with the name and DNS server you want to query.
PS> Test-DnsNameResolution.ps1 -Name DC.mylab.local -Server DC Not true
If the script
returns True the DNS server DC can resolve the name DC.mylab.local . If not, it would have returned the
The examples given here are only two out of thousands. The purpose of this article is not to show you how to perform infrastructure tests but rather to guide you in creating your own tests.
To create well-developed infrastructure tests, you must first define what it means to be "up" or "expected", build a PowerShell script to find the current and expected status and make a decision from it.
Don't keep manually confirming that your infrastructure is working as you expect. Learn how to build some PowerShell scripts and automate those everyday processes for you!