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.