The Power of Regex in PowerShell

mitchellreport-2010-05-21.jpgPowerShell is probably banned in MLB as a performance enhancing substance. You won’t find red-blooded American baseball player admitting to knowingly using PowerShell. It doesn’t matter what his coach and former teammates say. “I’m clean.” he’ll say to the public. “I didn’t come to talk about the past.” he’ll say in a prepared statement to Congress.

Ok, enough with the stretched analogy.

Power is as PowerShell Does

As it’s name implies, Powershell is extremely powerful. You can do a lot with it right out of the box. It’s built on the .NET framework and is inherently aware of the Windows operating system. This makes accessing system resources almost trivial assuming you have the appropriate permissions.

When combined with Regular Expression (regex), PowersShell’s capabilities are taken to a whole new level. Folders can be readily searched for files that contain a string of a certain format, such as an email address or an IP address.

Let’s consider a simple example to illustrate how PowerShell may be used by SQL Server DBAs.

Searching the SQL Server ErrorLog

SQL Server records information about its performance and status in its error log files. Database Administrators can examine these file for any anomalies that may have occurred. The problem is that there is a lot of chaff with wheat. DBAs must sift through a lot of normal information to find the relatively few exceptions. This is where PowerShell combined with regex can help out.

Let’s say we want to look through the ErrorLog for any errors with a severity level of greater than 9. The file can be multiple megabytes in size so even loading it into a text editor and doing a Find may be cumbersome. Plus that’s a very manual approach that is not easily automated.

So, we create a short PowerShell script to do this for us.

$log = get-content “E:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG”
$search = “Severity: \d{2}”
$log | select-string -pattern $search

What does this script do?

The first line uses the get-content cmdlet to read the log file into a variable named $log.

The second line defines the search pattern in a variable named $search. We are looking for “Severity: ” followed by two numbers.

The last line passes the contents of the log file into the select-string cmdlet and parses it using the search filter that we defined in line two.

The output can be seen below.

powershell-2010-05-21.jpg

That’s a lot easier than searching through the file by hand.

From here, we could view the information when we run the script, schedule the script to run automatically and save the information into an Excel spreadsheet, or email us the results in html format. I’ll cover some of those topics in another post.

Note: This script was written using PowerShell version 1.0 since that’s what I had readily available at the time of this post. The same script should run under version 2.0 of PowerShell with little, if any, modifications. Before the next post, I’ll upgrade to the newer version.

Question:
What have you done with regex in PowerShell. Share a link to your scripts in the comments below.

A Book Review of Tribes by Seth Godin

3257386945_2ba7f5c26d.jpg

Tanning beds, video rentals, and tax preparation seemingly have little in common. Yet many businesses in the great state that I call home have all three of these under one roof. Stop by, get your taxes done while you soak in some artificial rays, and go home with the latest Reese Witherspoon movie. Is that convenient or what?

Unfortunately, many technical communities operate in much the same way. They try to be everything to everyone instead of focusing on the core group of people that are passionate about the focus of the community

In his book, Tribes, Seth Godin takes a fresh look at the importance and dynamics of community, or as he calls it “a tribe”. “A tribe is a group of people connected to one another, connected to a leader, and connected to an idea. For millions of years, human beings have been part of one tribe or another. A group needs only two things to be a tribe: a shared interest and a way to communicate.”

Although this book isn’t written specifically for nor about the SQL Server community, there is much we can learn from his perspectives.

A Call to Action

Much of the book is a “call to action”. Godin believes that there are lots of people out there looking for a good community to join. He also points out that those same people are very tired of the status quo. They have grown weary of large sluggish communities that seem content to just survive. They want something new, something fresh, something empowering.

He encourages everyone, regardless of their current position within an organization or community to start acting boldly and to challenge the status quo. He proposes that we should all begin thinking like what he has termed a heretic. He believes that to continue going along in the same manner is to accept a slow and certain demise. People are looking for new and better, not more of the same.

He recognizes that it takes courage to step out and be willing to try something new, to think differently.

At the same time, Godin states that “the largest enemy of change and leadership isn’t a ‘no.’ It’s a ‘not yet.’ ‘Not yet’ is the safest, easiest way to forestall change. ‘Not yet’ gives the status quo a chance to regroup and put off the inevitable for just a little while longer. Change almost never fails because it’s too early. It almost always fails because it’s too late.”

Leading a Community

51AaZmgbjLL._SL160_.jpgIn Godin’s eyes, leadership of a community is rather straightforward. As he puts it: “The secret of leadership is simple: Do what you believe in. Paint a picture of the future. Go there. People will follow.”

That, of course, must be balanced with sound decisions and a demonstrated love for the community. Otherwise “people won’t follow you if they don’t believe you can get to where you say you’re going.”

Further he believes that leaders of a community must be transparent, completely transparent, because tribe members are savvy and know when something is amiss.

People don’t believe what you tell them. They rarely believe what you show them. They often believe what their friends tell them. They always believe what they tell themselves. What leaders do: they give people stories that they can tell themselves. Stories about the future and about change.

Growing the Community

Godin challenges many of the traditional ideas that have underpinned much of the thoughts around building and sustaining a community over the years. He pointedly asserts that a growth strategy for the sake of growth is short-sighted and leads to a community with little passion and endurance.

Instead he contends that community leaders should “focus on the tribe and only on the tribe”. When you focus on growth, you neglect the existing community.

He purports that a true leaders provide the community with a platform for spreading good ideas. Leaders exist to enable the tribe. When you are doing the right things, when you are focusing on the tribe, you’ll create an active and engaging community. And those kinds of communities grow naturally. “Tribes grow when people recruit other people.”

It’s tempting to make the tribe bigger, to get more members, to spread the word. This pales, however, when juxtaposed with the effects of a tighter tribe. A tribe that communicates more quickly, with alacrity and emotion, is a tribe that thrives.

In Conclusion, My Fellow Community Members

If you are a member of the SQL Server community, and you are if you know that you use SQL Server, reading this book will serve you well. I’m not suggesting that you’ll agree with it all or that something on every page will jump out at you and make you scream “Well, yeah!”

But a vibrant community is in all of our best interests. And this book will help spur ideas for running better user groups, SQLSaturdays, and even PASS.

Follow

Get every new post delivered to your Inbox.