Terminating Process in Terminal Server Environment

  • Thread starter Matthias Jarling
  • Start date
M

Matthias Jarling

Hi,

The following code to end processes works fine, but terminates the processes
of *all* logged on users (if the code is executed by an admin account). I
just want to kill the current users task.

--
Public Function funQuitProcesses(ByVal strProcessName As String) As Boolean
If Len(strProcessName) > 0 Then
Dim objWMI As Object
Set objWMI = GetObject("winmgmts:")

Dim strWMISQL As String
strWMISQL = "SELECT * FROM win32_process WHERE name = '" &
strProcessName & "'"

Dim objProcesses As Object
Set objProcesses = objWMI.ExecQuery(strWMISQL)

Dim objProcess As Object

For Each objProcess In objProcesses
objProcess.Terminate
Next

Set objProcesses = Nothing
Set objWMI = Nothing

funQuitProcesses = True
Else
funQuitProcesses = False
End If
End Function
--

What's the correct where-clause for this? Should be something like 'where
sessionid = " & TheUsersCurrentSessionID ' or 'where "processowner = " &
CurrentUser()'

thanks in advance, MJ
 
T

Torgeir Bakken \(MVP\)

Matthias said:
Hi,

The following code to end processes works fine, but terminates the processes
of *all* logged on users (if the code is executed by an admin account). I
just want to kill the current users task.

--
Public Function funQuitProcesses(ByVal strProcessName As String) As Boolean
If Len(strProcessName) > 0 Then
Dim objWMI As Object
Set objWMI = GetObject("winmgmts:")

Dim strWMISQL As String
strWMISQL = "SELECT * FROM win32_process WHERE name = '" &
strProcessName & "'"

Dim objProcesses As Object
Set objProcesses = objWMI.ExecQuery(strWMISQL)

Dim objProcess As Object

For Each objProcess In objProcesses
objProcess.Terminate
Next
Hi,

objProcess.GetOwner should do the job, something like this (strUser
would be an input parameter to the function):

For Each objProcess In objProcesses

lngRet = objProcess.GetOwner(strProcUser, strProcDomain)
If lngRet = 0 Then
If LCase(strProcUser) = LCase(strUser) Then
objProcess.Terminate
End If
End If

Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top