As with most of my Windows 2008 administration scripts I prefer to only "touch" the system if it needs updating otherwise leave it alone.  The script below shows how to detect if a service is set to manual, change to automatic and start if required.  Oddly the Get-Service cmdlet doesn't return the start type in PowerShell 2.0 for some reason.  

 

 function SetServiceStartToAutoMatic($serviceName = $(throw "Service Name was not specified")){  
      $service = Get-Service $serviceName  
      $startType = Get-WmiObject -Query "Select StartMode From Win32_Service Where Name='$($serviceName)'"  
   
      if ($startType.StartMode -eq "Manual"){  
           Set-Service $serviceName –StartupType Automatic  
      }  
 }  
   
 function StartServiceIfStopped($serviceName = $(throw "Service Name was not specified")){  
      $service = Get-Service $serviceName  
      if ($service.Status -eq "Stopped"){  
           Start-Service $serviceName  
      }  
 }  
   
 function Main(){  
      $smtpServiceName = "SMTPSVC"  
      SetServiceStartToAutoMatic $smtpServiceName  
      StartServiceIfStopped $smtpServiceName  
 }  
   
 Main  

 


Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Calendar

«  May 2012  »
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
View posts in large calendar

Recent Comments

Banners

Theme Grabber
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Dan Gibbons .Net Developer