Book Image

PowerShell for SQL Server Essentials

By : Donabel Santos
Book Image

PowerShell for SQL Server Essentials

By: Donabel Santos

Overview of this book

Table of Contents (15 chapters)
PowerShell for SQL Server Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Implementing Reusability with Functions and Modules
Index

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 type, event ID, entry type, and message. The following is the whole script:

Import-Module SQLPS -DisableNameChecking

$logname = "Application"
$source = "SQL Server Custom"

#current server name
$servername = "ROGUE"

$server = New-Object "Microsoft...