Solved

need help to modify script that autochecks windows services.

Posted on 2006-11-30
6
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 71

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 71

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 71

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 71

Expert Comment

by:Chris Dent
ID: 18045848

Glad I could help :)

Chris
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 96
VB6 - Convert HH:MM into Decimal 8 75
Using an encrypted  value to decrypt and display contents in vb6 9 63
VB6 Enviroment 3 35
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

732 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