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
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 = "email1, email2" ######################################################### # SMTP properties ######################################################### $emailFrom = "emailfrom" $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>
"@
The script can be found and download on Microsoft TechNet Gallery: http://gallery.technet.microsoft.com/disk-spaces-in-your-df94e274
Note: This type of script must be viewed as a complement to the tools mentioned above or used in the absence of them.
