Monitor your BizTalk environment using PowerShell – Disk Space Monitoring

Posted: August 26, 2012 in BizTalk, PowerShell
Tags: , , , ,

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 accomplished 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.
  • Many 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 disk 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

About these ads
Comments
  1. twh says:

    PowerShell newbie here. You would then schedule this script to run with the Windows scheduler?

    • 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 Scripts\DiskSpaceStorageReport.ps1′ “

  2. jNizM says:

    Great Script! How can i enable to see all drives not only $warningThresholdSpace & $criticalThresholdSpace.

    • 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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s