Solved

VB Script to pull the list of services running and the version of its executables.

Posted on 2011-09-09
2
486 Views
Last Modified: 2012-05-12
Hi Scripting Gurus I've wrote a script to pull the list of services running on a list of servers and export the details to a csv file with the service name service status and service executables file version.


Problem is that some executable paths have some switches and in those scenario script is unable to pull the exe's file version see some samples below. How to pull the file information as well in the script.


C:\Windows\System32\svchost.exe -k LocalSystemNetworkRestricted      
C:\Windows\System32\svchost.exe -k LocalServiceNetworkRestricted      
C:\Windows\system32\svchost.exe -k AxInstSVGroup      
C:\Program Files\Microsoft Forefront Identity Manager\2010\Password Reset Client Service\PwdMgmtProxy.exe""      
C:\Program Files (x86)\Common Files\Macrovision Shared\FLEXnet Publisher\FNPLicensingService.exe""      







On Error Resume Next
Dim fso, ts
strOutput = "Service_Status.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile("servers.txt", 1, False)
Set ts = fso.CreateTextFile("Service_Status.csv")
fv = "File Version"
ts.WriteLine """Computer"",""Service Name"",""Service State"",""Executable Path"",""File Version"""

While Not input.AtEndOfStream
	strComputer = input.ReadLine
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colRunningServices 
	    ts.WriteLine """" & strComputer & """,""" & objService.DisplayName & """,""" & objService.State & """,""" & objService.PathName & """,""" & objFSO.GetFileVersion(objService.PathName) & """"
If Err.Number <>0  Then 
	    ts.WriteLine """" & strComputer & """,""" & objService.DisplayName & """,""" & objService.State & """,""" & objService.PathName & """"
	    Err.Clear
	    End If 
	Next  
Wend

input.Close
ts.Close
Set ts = Nothing
Set fso = Nothing


MsgBox "Done. Please see " & strOutput

Open in new window

0
Comment
Question by:sujithmd
2 Comments
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36514802
Give this a try.  I think the only thing you can key off of to find the end of the program name is the ".exe".

On Error Resume Next
strOutput = "Service_Status.csv"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile("servers.txt", 1, False)
Set ts = fso.CreateTextFile("Service_Status.csv")
fv = "File Version"
ts.WriteLine """Computer"",""Service Name"",""Service State"",""Executable Path"",""File Version"""

While Not input.AtEndOfStream
    strComputer = input.ReadLine
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")

    for Each objService in colRunningServices 
        PathName = GetPathName(objService.PathName)
        ts.WriteLine """" & strComputer & """,""" & objService.DisplayName & """,""" & objService.State & """,""" & PathName & """,""" & objFSO.GetFileVersion(PathName) & """"
        If Err.Number <> 0  Then 
            ts.WriteLine """" & strComputer & """,""" & objService.DisplayName & """,""" & objService.State & """,""" & PathName & """"
            Err.Clear
        End If 
    Next  
Wend

input.Close
ts.Close
Set ts = Nothing
Set fso = Nothing

MsgBox "Done. Please see " & strOutput

Function GetPathName(s)
    i = InStr(LCase(s), ".exe")
    If i > 0 Then GetPathname = Replace(Left(s, i+3), """", "")
End Function

Open in new window

~bp
0
 
LVL 5

Author Closing Comment

by:sujithmd
ID: 36531399
Excellent Job done
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

680 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