Thursday, June 28, 2007

HowTo: Enable disabled services

If you want to automate startups on services that might be disabled by GPO, you can use Win32_Service class and change properties like the startup (Automatic\Manual\Disabled) or start\stop the service.

In my case, I prefer to use themes on my XP machine at work but our GPO disables them so our machines look like NT desktops... it sucks ain't it?

So to overcome this, I placed the script in my startup to enable the Themes and start the service.

Code Snippet:

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate}!\\" & _
  4. strComputer & "\root\cimv2")
  5. Set colServiceList = objWMIService.ExecQuery _
  6. ("Select * from Win32_Service where Name = 'Themes'")
  7. For Each objService in colServiceList
  8. 'Wscript.Echo objService.Name
  9. errReturnCode = objService.Change( , , , , "Automatic")
  10. If objService.State <> "Running" Then
  11. objService.StartService()
  12. Else
  13. objService.StopService()
  14. Wscript.Echo "Stopping..."
  15. Wscript.Sleep 5000
  16. objService.StartService()
  17. Wscript.Echo "Applying Themes"
  18. Wscript.Sleep 5000
  19. End If
  20. Next
Change line #6 value to any services that you want to enable (my case it's Name = 'Themes').

I prefer to use Cscript when executing any vbs scripts to avoid having to click on message prompts whenever you Echo.

If at first you fail, call it version 1.0

0 comments: