SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

PowerSQL – Identify a Head of the Database Blocks and Generate Automated Alert across all listed SQL Instances

This is going to be a simplest way of capturing and sending some useful information via an email of ‘N’ number of servers. With a little modification, you can implement various database related automation and capture very useful information.

To Identify the SPID (Session ID) of a head of the blocking chain using SMO server object using method named EnumProcesses which lists all running processes in an instance. Once the SMO server object is instantiated, all you need to invoke is the EnumProcesses method.

Please refer by earlier post to load SMO’s, if it’s not loaded.


I’ve also used global variables for email sending option. First lets create a text file List.txt in which all instance names are stored and Change the MailServer, EmLst Parameters as per your configuration. Schedule a sql job to run it periodically so that an auto-generated alerts will be sent an intended administrators.

DBBlocks.ps1 – Copy and paste below content




$Dbblock = “dbblock.htm”

New-Item -ItemType file $Dbblock -Force

Function writeHtmlHeader
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “<html>”
Add-Content $fileName “<head>”
Add-Content $fileName “<meta http-equiv=’Content-Type’ content=’text/html; charset=iso-8859-1′>”
Add-Content $fileName ‘<title>Database Blocks Report</title>’
add-content $fileName ‘<STYLE TYPE=”text/css”>’
add-content $fileName “<!–”
add-content $fileName “td {“
add-content $fileName “font-family: Tahoma;”
add-content $fileName “font-size: 11px;”
add-content $fileName “border-top: 1px solid #999999;”
add-content $fileName “border-right: 1px solid #999999;”
add-content $fileName “border-bottom: 1px solid #999999;”
add-content $fileName “border-left: 1px solid #999999;”
add-content $fileName “padding-top: 0px;”
add-content $fileName “padding-right: 0px;”
add-content $fileName “padding-bottom: 0px;”
add-content $fileName “padding-left: 0px;”
add-content $fileName “}”
add-content $fileName “body {“
add-content $fileName “margin-left: 5px;”
add-content $fileName “margin-top: 5px;”
add-content $fileName “margin-right: 0px;”
add-content $fileName “margin-bottom: 10px;”
add-content $fileName “”
add-content $fileName “table {“
add-content $fileName “border: thin solid #000000;”
add-content $fileName “}”
add-content $fileName “–>”
add-content $fileName “</style>”
Add-Content $fileName “</head>”
Add-Content $fileName “<body>”
add-content $fileName “<table width=’100%’>”
add-content $fileName “<tr bgcolor=’#CCCCCC’>”
add-content $fileName “<td colspan=’7′ height=’25′ align=’center’>”
add-content $fileName “<font face=’tahoma’ color=’#003399′ size=’4′><strong>Database Blocks Report – $date</strong></font>”
add-content $fileName “</td>”
add-content $fileName “</tr>”
add-content $fileName “</table>”


# Function to write the HTML Header to the file
Function writeTableHeader

Add-Content $fileName “<tr bgcolor=#CCCCCC>”
Add-Content $fileName “<td width=’10%’ align=’center’>SERVER</td>”
Add-Content $fileName “<td width=’50%’ align=’center’>SPID</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>BLOCKING SPID</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>DATABASE</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>COMMAND</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>STATUS</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>LOGIN</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>HOSTNAME</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>CPU</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>MEMUSAGE</td>”
Add-Content $fileName “</tr>”

Function writeHtmlFooter

Add-Content $fileName “</body>”
Add-Content $fileName “</html>”

Function writeDBInfo

Add-Content $fileName “<tr>”
Add-Content $fileName “<td>$Server</td>”
Add-Content $fileName “<td>$SPID</td>”
Add-Content $fileName “<td>$BLOCKINGSPID</td>”
Add-Content $fileName “<td>$DATABASE</td>”
Add-Content $fileName “<td>$COMMAND</td>”
Add-Content $fileName “<td>$Status</td>”
Add-Content $fileName “<td>$Login</td>”
Add-Content $fileName “<td>$hst</td>”
Add-Content $fileName “<td>$CPU</td>”
Add-Content $fileName “<td>$Memusage</td>”
#<td bgcolor=’#FF0000′ align=center>
Add-Content $fileName “</tr>”

Function sendEmail

$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

writeHtmlHeader $Dbblock

#Use of Global Variable


function increment {

foreach ($server in Get-Content List.txt)
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $server
$blocks=$server.EnumProcesses() |Where {$_.BlockingSpid -ne 0 -and $_.spid -gt 50 }

if ($blocks -ne $NULL)
foreach ($db in $blocks)
Add-Content $Dbblock “<table width=’100%’><tbody>”
Add-Content $Dbblock “<tr bgcolor=’#CCCCCC’>”
Add-Content $Dbblock “<td width=’100%’ align=’center’ colSpan=10><font face=’tahoma’ color=’#003399′ size=’2′><strong> $server </strong></font></td>”
Add-Content $Dbblock “</tr>”
writeTableHeader $Dbblock
increment $global:a
write-host $Dbblock $server $db.Spid $db.BlockingSpid $db.Database $db.Command $db.Status $db.Login $db.host $db.cpu $db.Memusage
writeDBInfo $Dbblock $server $db.Spid $db.BlockingSpid $db.Database $db.Command $db.Status $db.Login $db.host $db.cpu $db.Memusage

Add-Content $Dbblock “</table>”

writeHtmlFooter $Dbblock

if ($global:a -ge 1)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
sendEmail powersql@powersql.com “$emlst” “Database Blocks Space Report – $Date” $MailServer $Dbblock

How to run the above file

- You can right click and execute

- Goto to PowerShell Console – .\DBBlocks.PS1



PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.


Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...