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
Solved

need help to modify script that autochecks windows services.

Posted on 2006-11-30
6
183 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

789 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