Modification of Get-SQLErrorLog

Apr 26, 2011 at 10:11 PM

Here a recommended update to the Get-SqlErrorLog function that will allow us access to the SQL Agent Error Log

function Get-SqlErrorLog
{
    param(
    [Parameter(Position=0, Mandatory=$true)] $sqlserver,
    [Parameter(Position=1, Mandatory=$false)] [int]$lognumber=0,
	[Parameter(Position=2, Mandatory=$false)] [int]$logtype=1
    )

    switch ($sqlserver.GetType().Name)
    {
        'String' { $server = Get-SqlServer $sqlserver }
        'Server' { $server = $sqlserver }
        default { throw "Get-SqlErrorLog:Param '`$sqlserver' must be a String or Server object." }
    }

    Write-Verbose "Get-SqlErrorLog $($server.Name)"
    if ($logtype -eq 1){
		$server.ReadErrorLog($lognumber)
		}
	elseif ($logtype -eq 2) {
		($server.JobServer).ReadErrorLog($lognumber)
	}
    
} #Get-SqlErrorLog