Read the SQL Server Error Log with PowerShell

This script uses a native client call, so you can use it on any machine that has PowerShell installed along with the SQL Server client software. As always, only run this script on a test system until you understand what it does, and of course you'll need to change the server name in the connection string below:

 # Connect and run a command using SQL Native Client, Returns a recordset
 # Create and open a database connection
 $sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=(local);database=master;Integrated Security=sspi"
 #Create a command object
 $sqlCommand = $sqlConnection.CreateCommand()
 $sqlCommand.CommandText = "xp_ReadErrorLog"
 #Execute the Command
 $sqlReader = $sqlCommand.ExecuteReader()
 #Parse the records, read all three columns returned
 while ($sqlReader.Read()) { $sqlReader[0], $sqlReader[1], $sqlReader[2] }
 # Close the database connection