Tip: Use C# to Stop a Windows Service and force kill its processes on a remote computer (LAN)

I hope this tip will save someone some time as I has spent a good deal of time trying to get this to work they way I wanted. Online findings were only partial solutions to my problem.

Recently, situations cropped up where certain Windows Services were getting stuck in a “StopPending” state and would not resolve no matter how much time was given.

About a year ago I needed a way to monitor windows services in case they went down and send out email notifications so, I wrote it.

I had a UI with a SQL Server back end that would allow me to control turning off and on windows services. I also wrote a monitoring service that ran on a server that would read the table and change the flags and actually do the work of turning off an on the services based on the flags. During that project I also needed the ability to change the StartupType of the service. I wanted it to be set as Manual when it was off and Auto when it was Running.

I found a DLL on Code Project for controlling the StartupType it was simply called ServiceControllerEx.
However, it did not work for a remote computer (another computer on the LAN not on the Internet). I was able to modify the code to connect to remote computers this way:
“\\\\” + this.MachineName + “\\root\\cimv2:Win32_Service.Name='” + this.ServiceName + “‘”

After that it worked great on a remote and the local servers just fine.

Now, back to the “StopPending” problems.

I needed my monitor in some situations to not only try to stop the service but stop any processes owned by the service.

I quickly found out that using Process.Kill() only works on the local machine. If you try to use this on a remote machine it says “Feature is not supported for remote machines.”  Researching that message brought me to this blog but did not fully cover the situation I was experiencing. While this would remotely kill the processes on a remote computer it did not cover how to limit it to only processes owned by a specific service.

Note: You also needed to use this method because it allowed you to use a Service Account with the appropriate permissions to control the processes on a remote machine.

I was finally able to solve this my adding a new method to the ServiceControllerEx class that would kill a services processes like this:
KillServiceProcessesEssentially, you can use ManagementObjectSearcher to do it all.

First, define the scope which is the machine name and the service account credentials.

Second, select the ProcessID of the service.

Third, iterate through all the processes on that machine and compare the ParentProcessID to the ProcessID and call the kill command if they match.

Good luck!!
-Russell

 

Amateur Radio Anniversary

This June marks my 13th year as an Amateur Radio Operator and 10th as Extra class.
I became an Amateur Radio operator in 1999, upgraded to Extra class in 2002.

While I’m not quite as active on air, I still find this one of the most interesting hobbies out there. I’m most involved with the Skywarn aspects and an active member of the ALERT club who works with the National Weather Service to help relay storm reports from the Amateur community.

Google Music

Finally playing around with Google Music after getting into the beta way before it released.

Concept is to upload your music collection to the service then it’s available to you via app or browser anywhere. After transferring for 4 days I now have access to my 14GB collection anywhere. If I’m on my phone I have the option to stream or make available offline anything I want.

It’s much easier to maintain playlist because I know if wont’ loose it since it’s in the cloud vs tied to a pc.

Google music solved everything I hated about iTunes and Double Twist. Yet another Google product that has changed the way I do things!

Google Listen

I’ve never been an Apple guy, but trying to use an old windows mobile 6.1 phone to run with and listen to podcast on was a horrible experience. A few years ago, I bought a used iPod nano 2nd gen and began using it for podcasting and Nike+ for running.

Unfortunately, about a month ago the iPod took a trip through the washer and is no more.

I had gone with Android for my last phone so I thought maybe give it a try instead of spending the ridiculous amount of money they want for a new apple device.

I came up with Google Listen.

I have to say I’ve been pretty impressed with it. I have to point out the barrier to entry is high, its probably not for someone who doesn’t know what they are doing or understands basic RSS feeds. However, I’m a developer, I live technology.

I found the search for a podcast really worthless. (I’ll be using TWIT as my example through the rest of the article.) I tried searching for TWIT – This Week in Tech and I found about everything under the sun that wasn’t it. It was in the list but if you didn’t know, you weren’t finding it.

Going to the podcast home page, I found, was the best way to get a RSS feed url.

Feed Url’s are the driving force behind Listen. When you go to a website and view the podcast feed you are looking for it to enclose the audio file (usually an mp3) in the feed.

Site links sometime setup like this:

What a feed looks like in Google reader Notice the “twit0309.mp3” shows and can be played in reader:

Notice my use of Google Reader, the backend sync for Listen is google reader. While it doesn’t sync your play time (meaning you can’t listen on in reader and listen or switch between and keep your place) but it give you an easy way to manage and add new feeds.

However, to get that to work properly, I ended up adding one feed manually in Listen:

Once i did, I now have a new folder available on google reader I can associate podcast feeds with for Listen specific feeds.


Let’s take a peek at the dead simple interface.

My listen items is anything available to listen too.
My subscriptions shows what you subscribe too.
The other too are worthless to me.

Fresh items are waiting for download or waiting to be organized in the queue.
Queued items work like an ordering system to how your podcast will play (order: top to bottom).

Menu and settings are pretty basic, i recommend only downloading on wi-fi though.

That’s it you are now enjoying your favorite Podcast/Netcast via Listen on your Android phone!

Oh, and I use Endomondo to track my running now (for Android and iPhone), it’s uses the GPS instead of the shoe sensor!