Finding Location of ErrorLog

Note: While this script will find the errorlog on all servers with a single SQL instance, it requires Powershell version 3 when multiple versions are present (looking into fixing it). We cannot use the #Require -Version parameter because it's only available on versions 4 and higher.

 

Finding the Location of the ErrorLog on a server can be done by reading the start up parameters of SQL.

The start up parameters  are stored in the Registry, however the location can change based on the version of SQL server, whether there is a default instance or named instance, or even if there are many SQL instances present on the server.  However, we know that parameters are stored inside:

$RegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL??*\MSSQLServer\parameters"

1-Registry

Once we find the exact Registry location, we need to consider that there maybe more than three entries there, depending on the number of start up parameters, which can be affected by the presence of trace flags, so we would want to return all the SQLArg entries.

The following commands return a PS Object with the Registry information:

$RegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL??*\MSSQLServer\parameters"

# Stores a list of the SQLArg Registry Keys
$Param1 = Get-Item $RegKey | Select-Object -ExpandProperty property

$Param1 | ForEach-Object { New-Object psobject -Property @{"property"=$_; "Value" = (Get-ItemProperty -Path $RegKey -Name $_).$_}} 

2-Objet_Errorlog

If we want to filter down and only get the ErrorLog information, we would run the following instead:

# Searches for ALL SQL servers installed on machine
$RegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL??*\MSSQLServer\parameters"

#  -e for errorlog.  -d for master file
$Filter = "-e"

# Stores a list of the SQLArg Registry Keys
$Param1 = Get-Item $RegKey | Select-Object -ExpandProperty property
# Creates an Object, and filters those starting with -e
# Finally, removes -e from the string
$Param2 = $Param1 | ForEach-Object { New-Object psobject -Property @{"property"=$_; "Value" = (Get-ItemProperty -Path $RegKey -Name $_).$_}} 


IF ( (($param1 | Where-Object {$_ -eq "SQLArg0"}).count -gt 1) -and ($PSVersionTable.PSVersion.Major -lt 3) )

{ 
write-host "sorry, not supported" 
}

ELSE
{

(($Param2 | Where-Object {$_.Value -like ($Filter+"*") }).Value).Replace($Filter,"")
}



3-Just_Errorlog

Leave a Reply

Your email address will not be published. Required fields are marked *