Solved

need help to modify script that autochecks windows services.

Posted on 2006-11-30
6
181 Views
Last Modified: 2010-04-30
Hi All,
 
I found this script on the web that checks my local pc for which automatic services are not running. What I would like to do is three things,
 
1. Output the results to a csv file
2. Check a list of servers from a txt file instead of my local pc and output
3. If possible, have a username and password box appear when someone needs to run the script. Although if this can't be done i can get around it by using a batch file to run the script.
 
Option Explicit
' Displays a list of Windows services that are set to start Automatically,
' but are not currently running.
'
Dim strComputer, wbemServices, wbemObjectSet, wbemObject, strMessage
On Error Resume Next
Set wbemServices = GetObject("winmgmts:\\.")
Set wbemObjectSet = wbemServices.InstancesOf("Win32_Service")
strMessage=""
For Each wbemObject In wbemObjectSet
  if wbemObject.StartMode = "Auto" Then
      if wbemObject.State <> "Running" Then
'       Wscript.Echo """" & wbemObject.Name & """,""" & wbemObject.State & """,""" & wbemObject.DisplayName & """"
        If strMessage<>"" Then strMessage=strMessage & ", "
        strMessage=strMessage & wbemObject.DisplayName
      End If
  End If
Next
If strMessage="" Then
 Wscript.Echo "None"
Else
 Wscript.Echo strMessage
End If
 
Thanks All
0
Comment
Question by:dcreedon
  • 4
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18044773

This should do it:


Option Explicit

Const COMPUTER_LIST = "Computers.txt"

Dim objFileSystem, objFile, objStream, objWMIService, objItem
Dim strComputer, strMessage
Dim colServices

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(COMPUTER_LIST)
Set objStream = objFile.OpenAsTextStream(1, 0)

Do While Not objStream.AtEndOfStream
      strComputer = objStream.ReadLine
      
      If strComputer <> "" Then
            On Error Resume Next
            
            Set objWMIService = GetObject("winmgmts:\\" & strComputer)
            Set colServices = objWMIService.InstancesOf("Win32_Service")
            
            For Each objItem In colServices
                  If objItem.StartMode = "Auto" Then
                        If objItem.State <> "Running" Then
                              strMessage = strMessage & strComputer & "," & objItem.DisplayName & "," &_
                                          objItem.State & VbCrLf
                        End If
                  End If
            Next
            
            Set colServices = Nothing
            Set objWMIService = Nothing
      End If
Loop

Set objStream = Nothing
Set objFile = Nothing

Set objFile = objFileSystem.OpenTextFile("Report.csv", 2, True, 0)

objFile.WriteLine strMessage

Set objFile = Nothing
Set objFileSystem = Nothing
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18044801

I should have noted that you would need to list the computers you want it to run on in the text file "computers.txt" as specified in the Constant at the beginning. The message writes out to Report.csv in the same folder as you run the script from.

Chris
0
 

Author Comment

by:dcreedon
ID: 18045066
Hi Chris,

Thanks for all your help so far. I have one other request that has come from the client that asked me to do this. If it is possible he wants to get the output in the following format

Done               Done                  Need               Need               Need               Done                
Servername  Service Name1 Service Name2  Service Name3  Service Name4 Stopped
Servername  Server Ok
Servername  Service Name1 Service Name2  Service Name3  Service Name4 Stopped

Date  
Time

etc...

There is a list of 422 server that he needs to check. I don't know if thats possible but any help is much appriceated.

Thanks
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 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 18045377

The last column would have to be per service really. It's possible they are stuck in Starting or Stopping and presumably you'd want to capture that?

The script below creates output like this:

ServerName ServiceName1 ServiceState1 ServiceName2 ServiceState2

Otherwise we can remove the status capture entirely and have:

ServerName ServiceName1 ServiceName2

If you prefer that you would need to replace:

      strMessage = strMessage & "," & objItem.DisplayName & "," & objItem.State

With:

      strMessage = strMessage & "," & objItem.DisplayName

Otherwise here's the modification:



Option Explicit

Const COMPUTER_LIST = "Computers.txt"

Dim objFileSystem, objFile, objStream, objWMIService, objItem
Dim strComputer, strMessage
Dim colServices

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(COMPUTER_LIST)
Set objStream = objFile.OpenAsTextStream(1, 0)

Do While Not objStream.AtEndOfStream
      strComputer = objStream.ReadLine
      
      If strComputer <> "" Then
            On Error Resume Next
            
            Set objWMIService = GetObject("winmgmts:\\" & strComputer)
            Set colServices = objWMIService.InstancesOf("Win32_Service")

            strMessage = strMessage & strComputer
            
            For Each objItem In colServices
                  If objItem.StartMode = "Auto" Then
                        If objItem.State <> "Running" Then
                              strMessage = strMessage & "," & objItem.DisplayName & "," & objItem.State
                        End If
                  End If
            Next
            
            strMessage = strMessage & VbCrLf
            
            Set colServices = Nothing
            Set objWMIService = Nothing
      End If
Loop

Set objStream = Nothing
Set objFile = Nothing

Set objFile = objFileSystem.OpenTextFile("Report.csv", 2, True, 0)

objFile.WriteLine strMessage

Set objFile = Nothing
Set objFileSystem = Nothing
0
 

Author Comment

by:dcreedon
ID: 18045627
Hi Chris,

Thanks for that. I'll accept this answer.

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 18045848

Glad I could help :)

Chris
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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

27 Experts available now in Live!

Get 1:1 Help Now