Solved

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

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA code for SubFolders 11 49
Import CSV with All modify groups 17 89
Classic ASP - Display returned sql output parameter 9 56
max value in vbscript 7 12
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
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…

739 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