Hey Posh, Are My Services Running?–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

In a previous post, I looked at escaping strings. The reason I needed to do this was that I was trying to do some automatic work with building and starting SQL Server instances. Part of laying the base for this was checking if services are running, and then perhaps taking action, like starting or stopping.

I knew there was a Get-Service command, and ran that. The output from this is much more than I’d like to consume.

2016-11-15 14_46_58-cmd - powershell (Admin)

I’d like to limit this to SQL Server services. I know there is usually an MSSQLServer service, but since I tend to use named instances, this doesn’t work. Plus, I don’t want to search for just a particular service. I want all services for SQL Server.

There is a Where-Object command, that allows me to search. There is also a –Like option for comparisons. I’ll structure a command like this:

Get-Service | Where-Object ($_.Name –Like “SQL*”)

That is less than successful.

2016-11-15 14_49_20-cmd - powershell (Admin)

Why not? Well, PoSh has some syntax requirements and one of them here is that I don’t want parenthesis, I want curly braces. If I change this, things work better.

2016-11-15 14_51_00-cmd - powershell (Admin)

If you’re older like me and don’t necessarily read small print easily, this might be one that catches you for a bit. However, notice that I only have my SQLAgent and system services, not the core database engine. My wildcard needs work.

2016-11-15 14_52_01-cmd - powershell (Admin)

Now I see all my services and I can easily decide if I want to stop, start, restart, etc.

#SQLNewBlogger

This was a quick post. It took me 10 minutes to relearn a few PoSh things and practice and then about 5 minutes to write this.

I’ll remember it, and it shows how I’m building my administrative skills. You should do that as well.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

2 Responses to Hey Posh, Are My Services Running?–#SQLNewBlogger

  1. sqlvariant says:

    Steve,
    This is a great example for you to use the -Like operator!

    If someone is just looking for ‘SQL’ services though, just do:
    Get-Service -DisplayName SQL*

    You will get a different set of services, but it’s probably the list of services you were looking for. I guess I should blog that, huh?

    Also, when you use -DisplayName you can use intellisense if you’re looking for a specific service.

Comments are closed.