Time to read: ~ 3 minutes
Date of writing: 2017-10-19
Everybody has dealt with them in one form or another; Log Files, Captain’s Logs, Yule Logs, the list goes on!
In my case, we have logs on SQL Server Agent jobs; whether they’ve completed successfully or not, as well as any output created through the running of whatever is in the jobs.
Since these logs are mainly to do with Database Maintenance, we use them to identify indexes, statistics, backups, or DBCC CHECKDBs that are taking a particularly long time to maintain so we can hopefully do something about them.
We send out notification emails if jobs fail as well but hey, we’re DBAs, we like a backup!
Since this used to be done manually, and since I like PowerShell, I created separate functions that can take individual log files and parse out the needed data in a nice, easy format.
My problem, and the reason for this post, was figuring out, based on the name of the file in a directory, which function to call…
Let us create some strings so everyone can play along at home.
The files were split out into different .txt files bases on what was run, and I needed to invoke a particular function depending on what was in the file name.
I’ve thrown that into a variable called
$fileNamesjust to call them easier e.g.
$filenames = 0..4 | ForEach-Object....
Since I don’t always know the exact string, I can’t use
-eq, I have to rely on checking for parts of the string.
Now I could have just gone with the simple choice and used what I know. This would have been using multiple
if / elseif statements with the
…but it’s not brief and I’ve been learning about the
switch operator so I want to use that.
What’s the point in learning about stuff if you’re not going to implement it, right?
That was the start of my problem…
When intuition proves me an idiot…
So I change the code away from the
if / elseif code to a
switch statement, added in a final clause in case I feck things up, and let it run…
Okay, that failed miserably. Let’s quickly glance at the help on the
switch operator and see what we’re doing wrong i.e.
Get-Help about_Switch -Full;
If we throw a
-regex before the value, we can check if it matches our string! Brilliant, let’s do that!
And it works! Brilliant!
That’s not all folks!
Why I like blogging is because I learn stuff, arguably more stuff than just by working through the problem.
Such as I need to read the help files more rather than just skimming through them…
If you think about the above examples, something is wrong. It seems that you can do a lot more with
if than you can do with
This can’t be the case, otherwise why would Microsoft promote it when you have multiple if statements?
I must be missing something…
So I go back and read through the help files again, all of it…
In my haste and false intuition, I assumed that
switch couldn’t take an expression…all because my intuition said I had to wrap my expression in brackets.
It should have been curly brackets!!!
Now in this case, I am happy with the
switch -regex as it’s shorter and more compact but, if the time came where I need to check on multiple conditions or need to use an expression in the
switch, it’s nice to know that I don’t have to revert to a whole heap of
if / elseif/ else statements.