?
Solved

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

Posted on 2011-09-09
2
Medium Priority
?
492 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
[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
2 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
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). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

719 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