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.
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.
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.
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.
Now I see all my services and I can easily decide if I want to stop, start, restart, etc.
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.