Logging blocked processes

In this example, we are going to see log blocking processes in the Windows Event Log. You will need to ensure that you are running this script with elevated privileges, that is, as administrator. This is the snippet to check for blocking processes:

$server.EnumProcesses() |
Where-Object IsSystem -eq $false |
Where-Object BlockingSpid -gt 0

To log using a custom source, you can add the following block to check if the source name exists, and, if not, create it:

#check if Event Log source exists, otherwise create
if(!([System.Diagnostics.EventLog]::SourceExists($source)))
{
    New-EventLog -LogName $logname -Source $source
}

The cmdlet that writes to the event log is Write-EventLog, and it requires the log name, source, event ...

Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.