Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Script that will record when ever there is any 1 service or more missing or stopped or Manual while startup.

Hi,

Script that will record when ever there is any 1 service or more missing or stopped or Manual while startup.
I have 6 services to check if all are there and started and Automatic. If anything is different i want the machine name and the status recorded into a UNC path log.

Can anyone help on this

Regards
sharath
Avatar of Jan Vojtech Vanicek
Jan Vojtech Vanicek
Flag of Czechia image

Avatar of TheNautican
TheNautican

try this one.
On Error Resume Next
Dim objFSO, objLog, strLogPath, strLog, strComputerName
 
'strLogPath = "\\Servername\Serverfolder\"
strLogPath = "C:\"
strLogFile = strLogPath & "ServiceError.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set objLog = objFSO.OpenTextFile( strLogFile, 2, True)
 
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
 
 
For Each procItem in GetObject("winmgmts:").InstancesOf("Win32_Service")
	If procItem.DisplayName = "Alerter" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If		
Next

Open in new window

Avatar of bsharath

ASKER

Thank U
These are the 6 services i need to monitor
("Sophos Agent",
              "SAVservice",
              "Sophos Device Control Service",
"SAVAdminService",
"Sophos Message Router",
              "Sophos AutoUpdate Service")

How can i add them to the code
could probably make in cleaner  but...
Also, this uses the Display name of the service to verify, so make sure that's what are gave me.
On Error Resume Next
Dim objFSO, objLog, strLogPath, strLog, strComputerName
 
'strLogPath = "\\Servername\Serverfolder\"
strLogPath = "C:\"
strLogFile = strLogPath & "ServiceError.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set objLog = objFSO.OpenTextFile( strLogFile, 2, True)
 
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
 
 
For Each procItem in GetObject("winmgmts:").InstancesOf("Win32_Service")
	If procItem.DisplayName = "Sophos Agent" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If
	
	If procItem.DisplayName = "SAVservice" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If
	
	If procItem.DisplayName = "Sophos Device Control Service" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If	
	
	If procItem.DisplayName = "SAVAdminService" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If	
	
	If procItem.DisplayName = "Sophos Message Router" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If	
	
	If procItem.DisplayName = "Sophos AutoUpdate Service" Then
		If procItem.StartMode <> "Auto" Then
			objLog.WriteLine strComputerName & ": " & procItem.DisplayName & " is " & procItem.State
		End If
	End If		
Next

Open in new window

I Tried no errors but when there is a service stopped it does not record when stopped and run
ASKER CERTIFIED SOLUTION
Avatar of TheNautican
TheNautican

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works perfect...But the log file in the UNC gets cleared each time a new one arrives.
But i want them to be stored into one file. I will have the script as a startup script so.... When run it runs on many machines at one time... So record all into the log file....
change Set objLog = objFSO.OpenTextFile( strLogFile, 2, True) to the line below.
Alos, you can remove my test service of system restore.
Set objLog = objFSO.OpenTextFile( strLogFile, 8, True)

Open in new window

Thank U