Category Archives: SQL

Get SQL Log Messages with Windows Powershell

Here’s a little something for the sysadmin that monitors SQL servers (who doesn’t?). This handy little function retrieves SQL log messages for a specified instance. You can also specify how many of the most recent messages to retrieve.

function Get-SQLLog
{
param([string]$Instance, [Int]$SizeLimit = 10)
If ($Instance -eq $null){Write-Warning “Syntax: Get-SQLLog [-Instance] [[-SizeLimit] ]”}
Else
{
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.Smo’) | Out-Null
$Sql = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) $Instance
$sql.ReadErrorLog() | select -last $SizeLimit
}
}

»crosslinked«