Solved

Looking for script to audit windows automatic services.

Posted on 2008-10-28
8
563 Views
Last Modified: 2012-05-05
Looking for script to audit windows automatic services, start the service if stopped and output the server and service that was in stopped status to a text file or html file.
0
Comment
Question by:8AVega
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:weellio
ID: 22827561
taking a guess you want this to reside on one system then connect to remote systems and query them, then return the results?
how often would you run this?
how would you obtain the server names?


why not turn on the service recovery option so that it automatically restarts the service, then look at the event logs on your systems for any service issue?

old script, but still very handy for looking at eventlogs
http://cwashington.netreach.net/depo/view.asp?Index=1109&ScriptType=vbscript

' -------------------------------------------------------' 

Option Explicit

Dim objWMIService, objItem, objService, strServiceList

Dim colListOfServices, strComputer, strService
 

'On Error Resume Next
 

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

		& "{impersonationLevel=impersonate}!\\" _

		& strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _

		("Select * from Win32_Service ")
 

For Each objService in colListOfServices

	if objService.Startmode = "Auto" then

		if objService.state = "Stopped" then

			objService.StartService()

			strServiceList = strServiceList & _

			"**********" & objService.name & "**********" & vbcrlf

		end if

	End if

'		strServiceList = strServiceList & _

'			objService.name & "       " & _

'			objService.state & "       " & _

'			objService.Startmode & vbCrlf 

Next
 

WScript.Echo strServiceList
 

'useful  info

'objService.StopService()

'objService.StartService()

'objService.state

'objService.name

'objService.Startmode

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22827568
Hi, try this.

Regards,

Rob.
Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20
 

arrComputers = Array("D09790RING")
 

strLogFile = "AutoServicesStopped.txt"
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objLog = objFSO.CreateTextFile(strLogFile, True)
 

objLog.WriteLine "Script started " & Now & VbCrLf & "================================"
 

For Each strComputer In arrComputers

	objLog.WriteLine "Connecting to " & strComputer & VbCrLf & "================================"

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

	Set colItems = objWMIService.ExecQuery("SELECT DisplayName FROM Win32_Service WHERE StartMode='Auto' And State<>'Running'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

	

	For Each objItem In colItems

		intReturn = objItem.StartService()

		If intReturn = 0 Then

			objLog.WriteLine objItem.DisplayName & " has been started."

		Else

			objLog.WriteLine objItem.DisplayName & " failed to start. Return code: " & intReturn

		End If

	Next

Next
 

MsgBox "Done"

Open in new window

0
 

Author Comment

by:8AVega
ID: 22858550
Rob, your script has most of what I'm looking for. Their is only one very important option missing. I have to check services for over 350 servers manually. Is it possible to add the servers to a text file or csv file and have the script run againt each server in the .txt or .csv file.  The output txt file will continue to provide the servers that required the service to start.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22859280
Sure, try this out.  I already had it coded to use an array, so now we can just fill the array with the computer names from a text file.

Regards,

Rob.
Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20
 

strLogFile = "AutoServicesStopped.txt"

strComputersFile = "Computers.txt"
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Const intForReading = 1

Set objFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

arrComputers = Split(objFile.ReadAll, VbCrLf)

 

Set objLog = objFSO.CreateTextFile(strLogFile, True)

 

objLog.WriteLine "Script started " & Now & VbCrLf & "================================"

 

For Each strComputer In arrComputers

	If strComputer <> "" Then

		If Ping(strComputer) = True Then

			objLog.WriteLine VbCrLf & "Connecting to " & strComputer & VbCrLf & "================================"

			On Error Resume Next

			Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

			If Err.Number = 0 Then

				On Error GoTo 0

				Set colItems = objWMIService.ExecQuery("SELECT DisplayName FROM Win32_Service WHERE StartMode='Auto' And State<>'Running'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

				

				For Each objItem In colItems

					intReturn = objItem.StartService()

					If intReturn = 0 Then

						objLog.WriteLine objItem.DisplayName & " has been started."

					Else

						objLog.WriteLine objItem.DisplayName & " failed to start. Return code: " & intReturn

					End If

				Next

			Else

				objLog.WriteLine VbCrLf & strComputer & " failed to accept WMI connection."

			End If

		Else

			objLog.WriteLine VbCrLf & strComputer & " failed to respond to ping."

		End If

	End If

Next

 

MsgBox "Done"
 

Function Ping(strComputer)

	Dim objShell, boolCode

	Set objShell = CreateObject("WScript.Shell")

	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)

	If boolCode = 0 Then

		Ping = True

	Else

		Ping = False

	End If

End Function

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 22859282
Whoops, forgot to clear the error....
Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

 

strLogFile = "AutoServicesStopped.txt"

strComputersFile = "Computers.txt"

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Const intForReading = 1

Set objFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

arrComputers = Split(objFile.ReadAll, VbCrLf)

 

Set objLog = objFSO.CreateTextFile(strLogFile, True)

 

objLog.WriteLine "Script started " & Now & VbCrLf & "================================"

 

For Each strComputer In arrComputers

	If strComputer <> "" Then

		If Ping(strComputer) = True Then

			objLog.WriteLine VbCrLf & "Connecting to " & strComputer & VbCrLf & "================================"

			On Error Resume Next

			Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

			If Err.Number = 0 Then

				On Error GoTo 0

				Set colItems = objWMIService.ExecQuery("SELECT DisplayName FROM Win32_Service WHERE StartMode='Auto' And State<>'Running'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

				

				For Each objItem In colItems

					intReturn = objItem.StartService()

					If intReturn = 0 Then

						objLog.WriteLine objItem.DisplayName & " has been started."

					Else

						objLog.WriteLine objItem.DisplayName & " failed to start. Return code: " & intReturn

					End If

				Next

			Else

				Err.Clear

				On Error GoTo 0

				objLog.WriteLine VbCrLf & strComputer & " failed to accept WMI connection."

			End If

		Else

			objLog.WriteLine VbCrLf & strComputer & " failed to respond to ping."

		End If

	End If

Next

 

MsgBox "Done"

 

Function Ping(strComputer)

	Dim objShell, boolCode

	Set objShell = CreateObject("WScript.Shell")

	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)

	If boolCode = 0 Then

		Ping = True

	Else

		Ping = False

	End If

End Function

Open in new window

0
 

Author Comment

by:8AVega
ID: 22874517
I'll test tomorrow and get back with you. Thank You....
0
 

Author Closing Comment

by:8AVega
ID: 31510652
Good morning Rob,

Just want to apoligize on my delay to accept your solution for "Looking for script to audit windows automatic services." I just want to thank you for your time and hard work. I have recovered from a difficult time today and accepted your solution as soon as I could.
Thanks again, you have talent.
0
 

Author Comment

by:8AVega
ID: 23191646
Robert Sampson, I was wondering if you could walk the services script for me so I can understand the flow. Thx....
The solution you provided starts auto services on servers listed on a text file.
THX

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

 

strLogFile = "AutoServicesStopped.txt"

strComputersFile = "Computers.txt"

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Const intForReading = 1

Set objFile = objFSO.OpenTextFile(strComputersFile, intForReading, False)

arrComputers = Split(objFile.ReadAll, VbCrLf)

 

Set objLog = objFSO.CreateTextFile(strLogFile, True)

 

objLog.WriteLine "Script started " & Now & VbCrLf & "================================"

 

For Each strComputer In arrComputers

	If strComputer <> "" Then

		If Ping(strComputer) = True Then

			objLog.WriteLine VbCrLf & "Connecting to " & strComputer & VbCrLf & "================================"

			On Error Resume Next

			Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

			If Err.Number = 0 Then

				On Error GoTo 0

				Set colItems = objWMIService.ExecQuery("SELECT DisplayName FROM Win32_Service WHERE StartMode='Auto' And State<>'Running'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

				

				For Each objItem In colItems

					intReturn = objItem.StartService()

					If intReturn = 0 Then

						objLog.WriteLine objItem.DisplayName & " has been started."

					Else

						objLog.WriteLine objItem.DisplayName & " failed to start. Return code: " & intReturn

					End If

				Next

			Else

				Err.Clear

				On Error GoTo 0

				objLog.WriteLine VbCrLf & strComputer & " failed to accept WMI connection."

			End If

		Else

			objLog.WriteLine VbCrLf & strComputer & " failed to respond to ping."

		End If

	End If

Next

 

MsgBox "Done"

 

Function Ping(strComputer)

	Dim objShell, boolCode

	Set objShell = CreateObject("WScript.Shell")

	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)

	If boolCode = 0 Then

		Ping = True

	Else

		Ping = False

	End If

End Function

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now