Solved

need help to modify script that autochecks windows services.

Posted on 2006-11-30
6
180 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 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

9 Experts available now in Live!

Get 1:1 Help Now