Monitor your BizTalk environment using PowerShell – Disk Space Monitoring

One of the principal needs for BizTalk Administrators is the ability to monitor the health of BizTalk environments and react promptly to possible problems, you can accomplish this by using certain tools such as:

  • BizTalk Administration Console: is a Microsoft Management Console (MMC) that you can use to manage and monitor BizTalk Server, and that you can use to deploy and manage your BizTalk Server applications.
  • BizTalk360: that provides a nice user-friendly interface to monitoring and supporting your BizTalk environments
  • SCOM (System Center Operation Manager) that can provide comprehensive monitoring for Windows systems and with the additional of BizTalk Server 2010 Management Pack, SCOM can offer also monitoring capabilities for BizTalk artifacts and BizTalk-related platform components.
  • Much more…

However, unfortunately, many times, some of these tools are not available for us but we still need to accomplish this task.

So how can PowerShell help us?

Windows PowerShell is a Windows command-line shell designed especially for system administrators. It includes an interactive prompt and a scripting environment that can be used independently or in combination. PowerShell can be used by BizTalk administrators to help them in automating tasks and monitor certain resources or operations.

In this post, I will explain how you can be able to monitoring disk spaces in your environment (BizTalk/SQL/Other machines) using PowerShell.

This script allows you to set:

  • A range of machines you need to monitor
#########################################################
# List of computers to be monitored
#########################################################
param (
      $serverList =  "C:\\Machine.txt"
)
$computers = Get-Content $serverList

The Machine.txt is a simple text file with the list of all machine names you want to monitor:

servername1
servername2
servername3
  • Configure disk free space warning and critical level
#########################################################
# Configuration of alarmists
#########################################################
[decimal]$warningThresholdSpace = 20 # Percentage of free disk space - Warning (orange).
[decimal]$criticalThresholdSpace = 10 # Percentage of free disk space - critical (red)
  • And configure your email notification settings
#########################################################
# List of users who will receive the report
#########################################################
$mailto = "mail1@mail.net, mail2@mail.net"

#########################################################
# SMTP properties
#########################################################
$emailFrom = "suport@mail.net"
$smtpServer = "mySMTPServer" #SMTP Server.
$smtpUsername = "myUsername"
$smtpPassword = "myPassword"

The script will monitor the disk space in all machines and you will receive an email with a list of all disks that are below the threshold set. If all the disks are above the threshold set is not sent any email.

#########################################################
# Monitoring Process
#########################################################
[System.Array]$results = foreach ($cmp in $computers) {
 Get-WMIObject  -ComputerName $cmp Win32_LogicalDisk |
where{($_.DriveType -eq 3) -and (($_.freespace/$_.size*100) -lt $warningThresholdSpace) }|
select @{n='Server Name' ;e={"{0:n0}" -f ($cmp)}},
@{n='Volume Name' ;e={"{0:n0}" -f ($_.volumename)}},
@{n='Driver' ;e={"{0:n0}" -f ($_.name)}},
@{n='Capacity (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},
@{n='Free Space (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}},
@{n='Percentage Free';e={"{0:n2}%" -f ($_.freespace/$_.size*100)}}
}

We can format the result of the email so that it is presented in a nice way:

#########################################################
# Formating result
#########################################################
$tableStart="<table style='boder:0px 0px 0px 0px;'><tr><th>Server Name</th><th>Volume Name</th><th>Driver</th>
<th>Capacity (Gb)</th><th>Free Space (Gb)</th><th>Percentage Free</th></tr>"

$allLines=""
for($i=0;$i -lt $results.Length;$i++){
     #get das variáveis
     $servers=($results[$i] | select -ExpandProperty "Server Name"  )
     $volumes=($results[$i] | select -ExpandProperty "Volume Name" )
     $drives=($results[$i] | select -ExpandProperty "Driver" )
     $capac=($results[$i] | select -ExpandProperty "Capacity (Gb)" )
     $freeSpace=($results[$i] | select -ExpandProperty "Free Space (Gb)" )
     $percentage=($results[$i] | select -ExpandProperty "Percentage Free" )
     
     #alterna cores das linhas
     if(($i % 2) -ne 0){
         $beginning="<tr style='background-color:white;'>"
     }else{
         $beginning="<tr style='background-color:rgb(245,245,245);'>"
     }
     #controi o body
     $bodyEl ="<td> " + $servers+ " </td>" 
     $bodyEl+="<td> " + $volumes + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $drives + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $capac + " </td>"
     $bodyEl+="<td style='text-align:center;'> " + $freeSpace + " </td>"
     $fr=[System.Double]::Parse($freeSpace)
     $cap=[System.Double]::Parse($capac)
     if((($fr/$cap)*100) -lt [System.Int32]::Parse($criticalThresholdSpace)){
         $bodyEl+= "<td style='color:red;font-weight:bold;text-align:center;'>"+$percentage +"</td>"
     }
     else{
         $bodyEl+="<td style='color:orange;text-align:center;'>"+$percentage +"</td>"
     }    
     $end="</tr>"
     $allLines+=$beginning+$bodyEl+$end
}
$tableBody=$allLines
$tableEnd="</table>"
$tableHtml=$tableStart+$tableBody+$tableEnd

# HTML Format for Output 
$HTMLmessage = @"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Disk Space Storage Report</b></h1>
<p style='font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'>This report was generated because the drive(s) listed below have less than $warningThresholdSpace % free space. Drives above this threshold will not be listed.</p>
<br><br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;}
table{width:80%;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableHtml
</body>
"@

or present it in a simple way:

#########################################################
# Formating result
#########################################################
$tableFragment = $results | ConvertTo-HTML -fragment

# HTML Format for Output 
$HTMLmessage = @"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Disk Space Storage Report</b></h1>
<p style='font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'>This report was generated because the drive(s) listed below have less than $warningThresholdSpace % free space. Drives above this threshold will not be listed.</p>
<br><br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;}
table{width:80%;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableFragment
</body>
"@

You also can:

  • Sending authenticated email
#########################################################
# Validation and sending email
#########################################################
# Regular expression to get what's inside of's
$regexsubject = $HTMLmessage
$regex = [regex] '(?im)
'

# If you have data between
's then you need to send the email
if ($regex.IsMatch($regexsubject)) {
     $smtp = New-Object Net.Mail.SmtpClient -ArgumentList $smtpServer
      $smtp.credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword);
      $msg = New-Object Net.Mail.MailMessage
     $msg.From = $emailFrom
     $msg.To.Add($mailto)
     $msg.Subject = "Disk Space Alert for $computer"
     $msg.IsBodyHTML = $true
     $msg.Body = $HTMLmessage
      $smtp.Send($msg)
}
  • Or without authentication
#########################################################
# Validation and sending email
#########################################################
# Regular expression to get what's inside of's
$regexsubject = $HTMLmessage
$regex = [regex] '(?im)
'

# If you have data between
's then you need to send the email
if ($regex.IsMatch($regexsubject)) {
     send-mailmessage -from $emailFrom -to $mailto -subject "Disk Space Alert for $computer" -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $smtpServer
}

Report sample:

Disk-Space-Storage-Report-Email

Note: This type of script must be viewed as a complement to the tools mentioned above or used in the absence of them.

I also have to thank my coworkers at Devscope: Pedro Castro e Rui Machado for helping me developing this script

The script can be found and download on Microsoft TechNet Gallery:
Monitoring disk spaces in your BizTalk environment with PowerShell (5.3 KB)
Microsoft TechNet Gallery

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

5 thoughts on “Monitor your BizTalk environment using PowerShell – Disk Space Monitoring”

    1. Hi,

      Yes, you need to create a windows task scheduler to run the script, setting the timing for monitoring: every day, hour,… you can do it in the local BizTalk machine or by another machine in the domain.

      You can´t invoke powershell script from windows task scheduler, so you need to create a bat file that invoke the powershell script, some like: powershell.exe -command “& ‘E:Maintenance ScriptsDiskSpaceStorageReport.ps1’ “

    1. Hi,

      you have to change the query by removing the LessThen condition:
      Get-WMIObject -ComputerName $cmp Win32_LogicalDisk |
      where{($_.DriveType -eq 3) -and (($_.freespace/$_.size*100) -lt $warningThresholdSpace)

      to something like this:
      Get-WMIObject -ComputerName $cmp Win32_LogicalDisk |
      where{($_.DriveType -eq 3)

      1. Excellent. Can we have another threshold along with Orange, Red & Green where Green can indicate the safe or healthy state. please suggest.

Leave a Reply

Your email address will not be published. Required fields are marked *

BizTalk360
BizTalk Server

Over 500+ customers across
30+ countries depend on BizTalk360

Learn More
Serverless360
Azure

Manage and monitor serverless
components effortlessly

Learn More
Atomicscope
Business Users

Monitor your Business Activity in iPaaS
or Hybrid integration solutions

Learn More

Back to Top