Time to catch up

I’m not quite a decade behind, but I’m most certainly late to the PowerShell game. The latest reminder came via the daily SQLServerCentral email. They were re-running the Stairway to SQL PowerShell Level 1. This is something I’ve been meaning to get to for a while, had a bit of time and an idea for a project. Having a project always makes it easier for me to learn a new technology. I need to put ideas into practice for them to stick the best. Here’s my situation. I have several servers running Dynamics NAV and MSSQL. There are a number of services and they need to be stopped and started in a certain order. For a while I’ve been doing this manually as needed. It’s not a hard job, but why not make it easier?

I went through some of the introduction to PowerShell on the SSC site and feeling pretty good about it I jumped into looking a services on a dev server. I have a new server that’s all mine (for the time being) and nobody will complain when services stop without warning. I like having a playground all to my self, though it’s kind of lonely on the teeter totter.

All by myself
All by myself

To look into the services and start them I used the following. The middle line, Get-Member -Type Method, returns the options for the service.

$service = Get-WmiObject -ComputerName xxxxxx -Class Win32_Service -Filter "Name='MicrosoftDynamicsNavServer'"
$service | Get-Member -Type Method

After running the .stopservice I double checked on the server to ensure the ReturnValue of 0 really meant the service was stopped and it was. I changed the -Filter to SQLSERVERAGENT, ran $service.stopservice(), saw the 0 and checked on the server. Boom stopped. Lastly I changed the -Filter to MSSQLSERVER, ran $service.stopservice(), saw the 0 and checked on the server. Yet another stopped service. This is quite a successful day. I usually don’t match up success with stopped services, but that’s the metric I was using.

This is the success you are looking for.
This is the success you are looking for.

Getting the services running again was as easy as going switching to .startservice and running in the reverse order.

$service = Get-WmiObject -ComputerName xxxxxx -Class Win32_Service -Filter "Name='MSSQLSERVER'"

This dev server is a bit stripped down and doesn’t have all the services that some other boxes (or however you want to refer to a VM) do. There are more services and typing this all out each time wouldn’t be any faster than remoting in and clicking. Making it faster wasn’t the metric, for today, it was learning something. Making it faster will be a metric for another day and I’ll need to automate this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s