vbscript processes for current user
-
Hi, I can get the processes for the machine by this code
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
But how can I filter it so that I only have the processes for the current user? Now I check with getowner but I think it can be easyer Full testcode
Sub Main()
Dim a, p, nSet a = GetProcesses(FullUserName)
wscript.echo "Er zijn: " & a.count & " processen!"for n= 0 to a.count -1 wscript.echo a.item(n)'.Name
Next
End Sub
Function GetProcesses(User)
Dim sLogedinUser, strcomputer, strNameOfUser, strUserDomain
Dim objWMIService, colProcessList, objProcess
Dim colProperties
Dim colProcesses
dim nCountSet colProcesses = CreateObject("Scripting.Dictionary")
sLogedinUser = User
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objProcess In colProcessList
colProperties = objProcess.GetOwner(strNameOfUser, strUserDomain)
If sLogedinUser = strUserDomain & "\" & strNameOfUser Then
colProcesses.Add ncount, objProcess.name
nCount =ncount+1
End If
Next
Set GetProcesses = colProcesses
End FunctionFunction FullUserName()
Dim objNetworkSet objNetwork = CreateObject("WScript.Network")
FullUserName = objNetwork.userdomain & "\" & objNetwork.UserName
End FunctionJan
-
Hi, I can get the processes for the machine by this code
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
But how can I filter it so that I only have the processes for the current user? Now I check with getowner but I think it can be easyer Full testcode
Sub Main()
Dim a, p, nSet a = GetProcesses(FullUserName)
wscript.echo "Er zijn: " & a.count & " processen!"for n= 0 to a.count -1 wscript.echo a.item(n)'.Name
Next
End Sub
Function GetProcesses(User)
Dim sLogedinUser, strcomputer, strNameOfUser, strUserDomain
Dim objWMIService, colProcessList, objProcess
Dim colProperties
Dim colProcesses
dim nCountSet colProcesses = CreateObject("Scripting.Dictionary")
sLogedinUser = User
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objProcess In colProcessList
colProperties = objProcess.GetOwner(strNameOfUser, strUserDomain)
If sLogedinUser = strUserDomain & "\" & strNameOfUser Then
colProcesses.Add ncount, objProcess.name
nCount =ncount+1
End If
Next
Set GetProcesses = colProcesses
End FunctionFunction FullUserName()
Dim objNetworkSet objNetwork = CreateObject("WScript.Network")
FullUserName = objNetwork.userdomain & "\" & objNetwork.UserName
End FunctionJan
Easier?? How much easier do you want it?? You're doing it the correct way. There is no other way to filter the process list without comparing the username to what's in the process.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Easier?? How much easier do you want it?? You're doing it the correct way. There is no other way to filter the process list without comparing the username to what's in the process.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I was hoping that a where in :
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
was faster Thanks for the awnser Jan
Since the Owner doesn't show up in the properties of Win32_Process, a WHERE clause isn't possible.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I was hoping that a where in :
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
was faster Thanks for the awnser Jan
In addition to what Dave said: see http://msdn.microsoft.com/en-us/library/windows/desktop/aa390460(v=vs.85).aspx[^]