Published on

January 2, 2016

Exploring SQL Server with PowerShell

As a SQL Server enthusiast, I am always looking for new ways to enhance my skills and explore different aspects of the technology. Recently, I came across an interesting concept that intrigued me – working with SQL Server on a “Windows Server Core” machine without a graphical user interface (UI). This led me to delve into the world of PowerShell scripting and discover some fascinating possibilities.

One of the first challenges I encountered was finding a way to query the Windows Event Viewer without actually opening it. Typically, when troubleshooting error messages, I would rely on the Event Viewer to provide insights. However, on a Windows Server Core machine, this was not an option. So, I turned to PowerShell to see if I could accomplish the same task through scripting.

Here are some of the PowerShell commands I used to achieve my goal:

  1. List the event viewer logs on a given system:
  2. get-eventlog -list
  3. Select the newest 50 messages from the application log:
  4. Get-EventLog -LogName Application -newest 50
  5. Gather logs after a particular date:
  6. Get-EventLog -LogName Application -after 1/10/2016
  7. Select only the messages logged as “information” for a specific source (e.g., MSSQLSERVER) and copy the output to the clipboard:
  8. Get-EventLog -logname application -EntryType information -newest 50 -source *MSSQLSERVER* | clip
  9. Find the relevant source names to be used in a query:
  10. Get-EventLog -logname "Application" | Select-Object Source -unique

By leveraging the power of PowerShell and understanding the queries we need, we can easily retrieve the desired data from the Windows Event Viewer. This opens up a whole new world of possibilities for SQL Server DBAs.

I am curious to know how many SQL DBAs have already explored PowerShell and what they have been able to accomplish with it. If you have any experiences or insights to share, please let me know in the comments below.

Stay tuned for more exciting SQL Server explorations!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.