Listing SQL Server log errors
In this recipe, we will list SQL Server log errors.
Getting ready
Check your SQL Server log in Management Studio. This should be what our PowerShell script should report.
How to do it...
Let's check how we can list SQL Server errors using PowerShell:
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module, and create a new SMO Server object as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
#According to MSDN: #ReadErrorLog: returns A StringCollection system object #value that contains an enumerated list of errors from #the SQL Server error log. #Note we are using PowerShell V3 because of simplified #Where-Object syntax [datetime]$date = "2011-11-01" $server.ReadErrorLog...