I’ve started knocking up a function to return the last few lines of a sqlserver errorlog.

This is a little way from being finished….but I’ve already found it quite handy

function get-sqlerrorlog { 

  [CmdletBinding()]	
  Param( 
      [String] [alias("computer")] $ComputerName,
      [Int] [alias("lines")] $NumberOfLines = 5
  )

  write-verbose "Running function $([string]$MyInvocation.MyCommand.name)"
  
  Write-verbose "`$ErrorLogFolder: $ErrorLogFolder"

  $ErrorLogFolder = dir sqlserver:\sql\$ComputerName 

  # Todo: need to work out how it works more > 1 named instance. This just picks 1st
  [string]$ErrorLogFolder = $($ErrorLogFolder | select -first 1).errorlogpath
  Write-verbose "`$ErrorLogFolder: $ErrorLogFolder"

  $ErrorLogFolder = $ErrorLogFolder.replace(':', '$')
  Write-verbose "`$ErrorLogFolder: $ErrorLogFolder"

  $ErrorLogFolder = '\\' + $ComputerName + '\' + $ErrorLogFolder
  Write-verbose "`$ErrorLogFolder: $ErrorLogFolder"

  # Todo: it might be that the get-content could be speeded up by retrieving less lines
  # Todo: seperate this bit out into seperate function ?  
  get-content "$ErrorLogFolder\ERRORLOG" | select -last $NumberOfLines                                                                
 
}
set-alias gsel get-sqlerrorlog