'****************************************************
'		Script Witten by Larry Heintz
'		March 2006 www.windowsadminscripts.com
' This script will enumerate all services by name,
' current status,current start mode,path to the service
' executable and process id. This will written to a
' comma delimited text file.
'
' Script Usage:
' Enum Service on Single PC: cscript services.vbs /computer:[ComputerName]
' Enum Service on AD Domain: cscript services.vbs /adwg:[AD or Work Group Name]
'****************************************************
Dim args,computername,adwg,logPath
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
adwg = args.Item("adwg")
logPath = getLogPath()

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage:"
	wscript.echo "Enum Service on Single PC: cscript services.vbs /computer:[ComputerName]"
	wscript.echo "Enum Service on AD Domain: cscript services.vbs /adwg:[AD or Work Group Name]"
elseif args.exists("computer") then
	if computername = "" then
		computername = getComputer()
	else
		computername = computername
	end if
	Call enumServices(computername,1)
elseif args.exists("adwg") then
	Call enumComputers(adwg)
end if

Function enumServices(computername,flag)
On Error Resume Next
Dim objsvc,svccount,count
count = 0
wscript.echo "Connecting to " & ucase(computername)
set objsvc = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
			("SELECT * FROM Win32_Service")
if not (errorChecking (computername)) then
	for each svc in objsvc
		count = count + 1
		Call writetoLog(svc.displayname,svc.state,svc.startmode,svc.pathname,svc.processid,computername)
		if flag = 1 then
			wscript.echo svc.displayname & "," & svc.state & "," & svc.startmode
		end if
	next		
	wscript.echo "There are " & count & " Services on " & ucase(computername)
	wscript.echo "Disconnecting from " & ucase(computername)
	wscript.echo ""
end if
set objsvc = nothing
End Function

Function enumComputers(adwg)
On Error Resume Next
Dim objadwg,computer
Set objadwg = getobject("WinNT://" & adwg)
if not (errorChecking (computername)) then
	objadwg.Filter = Array("Computer")
	for each computer in objadwg
		Call enumServices(computer.name,0)
	next
end if
Set objadwg = nothing
End Function

Function writetoLog(displayname,state,startmode,pathname,processid,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(logPath & "\services_" & computername & ".txt", 8,True)
	objFSOwriteline.WriteLine(displayname & "," & state & "," & startmode & "," & pathname & "," & processid)
	objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
	temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

Function errorChecking(ComputerName) 
errorChecking = False 
if err.number <> 0 then
	wscript.echo "Unable to connect to " & ucase(ComputerName)
	err.Clear () 
	errorChecking = True
end if 
end Function
