Solved

need help to modify script that autochecks windows services.

Posted on 2006-11-30
6
184 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 121
using web browser with BING 40 136
MS SQL store procedure to calculate and return result 6 68
VB6 - Convert HH:MM into Decimal 8 70
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

756 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