Build Server Health using the power of Powershell | HCL Blogs
Type to SearchView Tags

Build Server Health using the power of powershell
Rabia Sharma | June 13, 2019

Tired of logging into your build server and then finding it to be down? Want to know the daily health of your build servers? Would an automated email on the same be perfect?

Have no fear when Powershell is here!

Embrace the power of Powershell in having this work done for you. Powershell is an extremely easy and powerful scripting language. It comes in-built with Windows and has a command-line plus ISE console. Anyone who has some knowledge of any programming language can easily write a script in Powershell. To know more about it please visit: You can use this scripting language for numerous types of automation depending on your need.

Powershell is an extremely easy and powerful scripting language

To use Powershell in checking build server health, all it takes is to use a simple cmdlet “Invoke-WebRequest” (Powershell 3 and onwards) or you can use “System.Net.WebRequest” class to do an HTTP/HTTPs requests. For example:

Invoke-WebRequest -Uri $URL
$request = [System.Net.WebRequest]::Create($URL)
[System.Net.WebResponse] $response = $request.GetResponse();

You may receive a timeout error sometimes when the requests take longer than required. In such cases, you may add timeout like this

$request = [System.Net.WebRequest]::Create($URL)
$request.Timeout=10000; #in Milliseconds.
[System.Net.WebResponse] $response = $request.GetResponse();

Moving on, use an if-else on getting the response code and checking of the site is up or down

if ($response.StatusCode -eq "200" -or $response.StatusCode -eq "OK"){
echo "Site - $URL is up (Return code: $($response.StatusCode) - $([int] $response.StatusCode))"
else {
echo "Site - $URL is down"

You may get into a situation when the response would give an exception, in such cases add a try catch.

try {
$request = [System.Net.WebRequest]::Create($URL)
[System.Net.WebResponse] $response = $request.GetResponse();
if ($response.StatusCode -eq "200" -or $response.StatusCode -eq "OK") {
echo "Site - $URL is up (Return code: $($response.StatusCode) - $([int] $response.StatusCode))"
else {
echo "Site - $URL is down”
} catch {
echo "Site - $URL is not accessible. Try again"

Sometimes the URLs require TLS1.2/1.1 for making a connection, in such a case you may use below line on top of your powershell script to get a successful response.

[Net.ServicePointManager]::SecurityProtocol = `

You may also end up in a scenario where you may receive below exception while running your script

Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."
At line:6 char:5
+ $response = $webclient.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException

In order to ignore the SSL Trust error, please add below line on top of your script

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Cool so far?

Now simply redirect the above output to a file using redirect ( >>) operator as shown below

if ($response.StatusCode -eq "200" -or $response.StatusCode -eq "OK") {
echo "Site - $URL is up (Return code: $($response.StatusCode) - $([int] $response.StatusCode))" > >
else {
echo "Site - $URL is down" > > Results.txt

You can use this file anywhere, for example, like an attachment in your email or in the email body, you can have it for debugging purpose as well. Now let’s move on to sending a daily status email to your team on the same using smtp.

$emailSmtpServer = ""
$emailSmtpServerPort = "587"
$emailSmtpUser = "****"
$emailSmtpPass = "****"
$emailFrom = "****"
$emailTo = "****"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$" ")
$emailMessage.Subject = "Build Server Health-Artifactory/Git/TeamCity"
$emailMessage.Body = "Dear user," + "`n`n" + "Below is the health status of your build servers" + "`n" + "`n" + "$body" + "Regards," + "`n" + "Build Team"
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $True
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )

A snippet of the email is as below


Description: Snapshot of how the automated email looks like

How to auto schedule this email

Create a basic task in Windows Task Scheduler and mention the number of times in a week or a day you want this email. You can also trigger the email on demand basis or on condition basis. Mention in the Actions tab that you want to execute a Powershell script along with the path of the script file. For example:

  • Select action as “Start a program”
  • In program/script mention “powershell”
  • In add arguments add -File E:\Study\BuildTools.ps1
Window Task

Description: Snapshot of task’s action tab in Task Scheduler

And we are all set!

Why use Powershell

There are numerous options and scripting languages available in the market to do the same thing for you. Some benefits I see in Powershell are:

  1. It is open source
  2. Writing code is quite simple as Powershell takes care of many things in a program internally; for example, variable declaration
  3. It has more functions than other tools like cmd or VBScript

You can save an enormous amount of time of all your team members and the people involved in maintaining the build servers. This is a good proactive approach and will help everyone in the long run.