'****************************************************
'		Script Witten by Larry Heintz
'		March 2006 www.windowsadminscripts.com
' This script will enumerate all running processes
' on a computer. It will write to screen just the
' process name. It will also write the following info
' to a comma delimited text file, process name,
' executable path, priority, sessionid, owner, handle
' count and thread count.
'
' Script Usage: processes.vbs /computer:[computername]
'****************************************************
Dim args,logPath,computername
Set args = Wscript.Arguments.Named
logPath = getLogPath()
computername = args.Item("computer")

if computername = "" then
	computername = getComputer()
else
	computername = computername
end if

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage: processes.vbs /computer:[computername]"
	wscript.quit
elseif args.exists("computer") then
	Call getProcessInfo
else
	wscript.echo "Script Usage: processes.vbs /computer:[computername]"
	wscript.quit
end if

Function getProcessInfo
On Error Resume Next
Dim objProcess,process,strNameOfUser
Call writetoLog("====== " & now & " ==============================================")
Call writetoLog("Name,Executable Path,Priority,Session Id,Owner,Handle Count,Thread Count")
'Call writetoLog("====== " & now & " ==============================================")
set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
				 ("Select * From Win32_Process")
	if not (errorChecking (computername)) then
		for each process in objProcess
			if process.name <> "System Idle Process" and process.name <> "System" then
				Return = process.GetOwner(strNameOfUser)
				wscript.echo process.name
				Call writetoLog(process.name & "," & process.executablepath & "," & process.priority & "," & process.sessionid & "," & strNameOfUser & "," & process.handlecount & "," & process.threadcount)
			end if
		next
	end if
set objProcess = nothing
End Function

Function writetoLog(comment)
Dim fso,objTextStream
set fso = CreateObject ("Scripting.FileSystemObject")
Set objTextStream = FSO.OpenTextFile(logPath & "\processes_" & computername & ".txt", 8,True)
	objTextStream.WriteLine(comment)
	objTextStream.close
Set objTextStream = 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)
	Call writetoLog("Unable to connect to " & ucase(ComputerName))
	err.Clear () 
	errorChecking = True
end if 
end Function
