• Horn Player – Computer Dude – Gig Pig …
  • Home
  • About
  • Blog
  • Services
  • Contact
  • Webmail
  • Terms

Robert A. Owen - Musician - Technology Leader

Music, Technology, Rants and Funnies - Randomness at its best!

  • Blog
  • Internet
    • IIS 6
    • IIS 7.5
    • Javascript
    • Web Design
      • WordPress
      • Coldfusion
  • Support
    • MicroSoft
      • Powershell
      • Surface
      • Windows10
      • Zune
    • PC-Care
    • Software
    • Virus
      • Email Scam
    • Vista Quest
    • Windows 7
  • Pictures
    • Instagram
  • Music
  • Friday Funnies
  • General
    • CoWorking
    • Gadgets
    • Politico
    • Random – TidBit
    • Rant
    • Review
    • Site of the Week
    • Useless
    • Xbox
      • Games
You are here: Home / Support / MicroSoft / Powershell to check and start a service

Powershell to check and start a service

December 9, 2014 By Robert Owen

Ok, this one is not funny.  My posts are few and far between these days and they are mostly limited to pictures or something I find funny.  But, I was going through one of my Onenote files tonight and ran across this powershell script I wrote a while back and thought I would share.

When I put this script together, I was having issues with Collabnet Subversion on one of my servers.  For some reason, the Collabnet Service was stopping each night and nothing was being logged anywhere to give me an idea of why it was stopping with such frequency.  It wasn’t a big deal, except for the fact that it prevented several of the development teams in my office from accessing subversion.  Other than that it was fine. They don’t need that. Picky, picky.  The “fix” for this was just to restart the service.  Once that was done, everybody was happy again.

With the lack of system and application logs for this stop.  I was forced to hit that server each day and start the service if it was stopped.  Well, let me tell you, that is a major time suck and pretty pointless activity.  I know, I know… you’re saying, “why don’t you spend a little extra time and fix the issue!?”  Well, I had several projects going on at the time and the fix was simple so its importance dropped down on my list. SHAME ON ME!  Yeah, yeah,,, anyway back to the topic.

Because logging in to a server just to check to see if a service is running, then start it if it is not is such a boring task, I thought I would script it.  Powershell is, after all, supposed to be used to replace boring, mundane tasks.  So, what do I need?  I need a script that I can schedule to run hourly, check the Collabnet service.  If it is running, GREAT! If it is not, BOO!  Start it up.  Next, I should probably keep tabs on this someway, So I need it to log when it checked the service and what it did each time.

function FuncCheckService{
param($ServiceName)
$arrService = Get-Service -Name $ServiceName
$Date = Get-Date -Format "MM/dd/yyyy HH:mm:ss"
$Start = "Started "
$Started = " service is already started."
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
($Date + " - " + $Start + $ServiceName) | Out-file D:\ServiceLogs\serviceStart.txt -append
}
if ($arrService.Status -eq "Running"){ 
($Date + " - " + $ServiceName + $Started) | Out-file D:\ServiceLogs\serviceStart.txt -append
}
}

FuncCheckService -ServiceName "CollabNetSubversionServer"

There it is… Pretty simple.  Created a function that could be reused to check any service name you pass it.  I get the date and time, then check the service.  If the service is status is ‘Running” then append the log file with ” {Date / Time}  {serviceName} service is already started.”   Or if the service status is “not equal” to running, then start it and append the log file with “{Date / Time} Started {ServiceName}”

Looking at this again now, I see a big flaw.  I’m checking the service status for  “not Equal” to “running” instead of checking to see if it is “Stopped”.  There could be a status of “Starting”, which is “not equal” to “Running” and the start command wouldn’t be able to do anything then the script would fail and probably not log anything.

You’re welcome to fix that when you use it. 🙂

 

© 2014, Robert Owen. All rights reserved.

Related posts:

Windows 10 – Forget Wireless Network Default ThumbnailMicrosoft Zune Update Default ThumbnailError – A problem on your computer is preventing updates from being downloaded Default ThumbnailComplete PC Back-up: Pretty dang cool!

Filed Under: MicroSoft, Powershell

Robert Owen A Horn Player turned salesman, turned Computer Consultant & Horn Player, Turn Network Engineer & Horn Player, Now Technology leader & Musician.... Well, How about a Horn player who does a lot of other stuff? More about me. FacebookInstagramLinkedInTwitter

To Stay up-to-date on all of my crazy Ramblings, enter your e-mail address below.

You'll be amazed!

Recent Posts…

  • What If? – Gig Edition
  • Feedback – two way street
  • Snow-pocalypse… and Cold
  • Snow-pocalypse 2025 – part 6
  • Snow-pocalypse 2025 – part 5

Recent Comments

  • David Gray on Create a Shortcut to open in your Alternate BrowserTwelve years on, this tip remains useful. Though I…
  • Sue K on Use the Snipping Tool to Capture MenusThanks you so much! I could never figure out how t…
  • aa on Increase IIS File Upload LimitsSuperb Robert..after wasting 2 days..this is perfe…
  • León on Windows 7 error 86Funciono para Windows 7 , también descubrí que la…
  • Nicole Larkin on Use the Snipping Tool to Capture MenusNine years later and your post is still helpful. T…

[footer_backtotop]
Content Copyright © 2025 OWEN CONSULTING · All Rights Reserved · Log in