Monthly Archives: February 2011

Restarting Services with PowerShell

Thought I’d share a script I use to reset services nightly. This script will also e-mail if something goes wrong on the restart (which hasn’t ever been the case so far).

This specific reason I built this is for a Tomcat server that has some problems with errand threads (I didn’t build it and can’t change it). The only solution remaining to clean up these threads is a restart of the Tomcat process.

The nice part about PowerShell versus something like a batch file is that with little scripting the PowerShell script will “wait” for the services to change.

Here is the code, substitute your settings where applicable. Feel free to modify as needed. It goes without saying that you should test this on a non-production server before using.

## Reference to System.Net for email alerts

function restart-service
param ($srvName)

## stop service
stop-Service $srvName
$service = get-service $srvName

if($service.status -ne “Stopped”)
email-admin($srvName + ” has failed to stop, please manually stop and restart.”)
write($srvName + ” has failed to stop, please manually start.”)

##start service
start-Service $srvname
$service = get-service $srvName
if($service.status -ne “Running”)
email-admin($srvName + ” has failed to start, please manually start.”)
write($srvName + ” has failed to start, please manually start.”)
write($srvName + ” has started.”)

##email if failed
function email-admin($body)
$mail = New-Object System.Net.Mail.MailMessage
$mail.From = “psScript@server”
$mail.To.Add(“[email protected]”)
$mail.Subject = “Service Restart”
$mail.Body = $body
$smtp = New-Object System.Net.Mail.SmtpClient(“”)

restart-service BITS

I save this file as restartService.ps1 in my c:\PSScripts folder.

In order to add this as a regular scheduled task I use the following command:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -nolog -command “&{c:\PSScripts\restartService.ps1}”

Happy restarting!

Share and Enjoy

PSA: Sleep Apnea (Follow-up)

So a few months ago I posted about sleep apnea and my experiences in getting diagnosed and treated.
It’s been about 3 months now and I have to say it took about 2 months to totally get used to the mask at night (see pic). One thing for me that was interesting is that it cured my insomnia right off the bat. I actually looked forward to sleeping again.

Opus 360

Some things I didn’t expect was to be exhausted during the transition. Maybe it was the adjustment to the mask, maybe it was playing sleep catch up? I don’t really know.
Now that some time has passed a can say that my morning headaches are a thing of the past. I feel rested, not hating the world when I get up. I can honestly say that it has been a game changer at home and with the kids. Not so much crabby daddy anymore.
This whole thing was definitely worth the trouble even though having the mask on is less than attractive.

Now my only worry is getting my CPAP through security at the airport (in a few weeks).

Share and Enjoy