Solved

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

Posted on 2011-09-09
2
474 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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Excellent Job done
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now