Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminate duplicate process (in logged owner)

Status
Not open for further replies.

question82

Technical User
Oct 15, 2009
8
PL
Hi,
Im writing a script.
This script terminate all process "notepat.exe" in logged owner.
But script must terminate one if hi run.

-----------------------------------------

strComputer = "."
Dim strUserName

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colTel1 = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")

For Each objItem in colTel1
strUserName = objItem.UserName
Next

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcessByName = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")

For Each objProcess in colProcessByName

colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)

If strUserDomain & "\" & strNameOfUser = strUserName Then

objProcess.Terminate()

end if
end if
Next
 
are you meaning you need to count how many notepad.exe are active with the current user? and leave one? or just close one and not worry about this one you close? if it is the later then an 'Exit For' right after the objProcess.Terminate() should do it? (i hate exit statements, they arent elegant)
 
If you running 3x "notepad.exe" script close two, and leave one.
But script must close two new process'es, and leave oldest process.
 
so, you can perhaps get the number of running process by doing a .Count on the colProcessByName. some collections dont support this? so you might have to increment a counter and loop though once
i = 0
For
i = i + 1

you will prob wwant to loop through once in order to determine the oldest notepad session anyway.
datOldest = CDate("01/01/2999")
For
If datOldest < Process.StartDate Then
datOldest = Process.StartDate
End If


then loop though one more time comparing the processes properties with your counter and datOldest
 
Use the creation date property of Win32_Process to help you close the newest processes.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
ok, I use this function, but if in comuter workered 4 users where opened 5 "notepad.exe" process ?

the date property eliminate all process from all logged users.
 
Hi question82,

this code with find all "notepad.exe" processes launch by the current user (remote or local) and terminate all but the oldest.

Code:
strComputer = "."

set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objNetwork = CreateObject("WScript.Network")
set colProcesses = objWMI.ExecQuery("Select * from Win32_Process Where Name = 'Notepad.exe'")
set dicProcesses = CreateObject("Scripting.Dictionary")

strUserName = objNetwork.UserName
datOldest = now

function convertDate(dtmDate)
	convertDate = cdate(mid(dtmDate, 5, 2) & "/" & mid(dtmDate, 7, 2) & "/" & left(dtmDate, 4) & " " & mid (dtmDate, 9, 2) & ":" & mid(dtmDate, 11, 2) & ":" & mid(dtmDate,13, 2))
end function 

for each objProcess in colProcesses
	dummy = objProcess.GetOwner(strOwner)
	if (strOwner = strUserName) then
		if (convertDate(objProcess.CreationDate) > datOldest) then
			dicProcesses.add objProcess.CreationDate, objProcess
		else
			datOldest = convertDate(objProcess.CreationDate)
		end if
	end if
next

for each objProcess in ColProcesses
	if (dicProcesses.Exists(objProcess.CreationDate)) then objProcess.Terminate()
next
 
This code work fine.
I have one question.

I Have terminal server, when worked 6 users.
In these accounts lanched 8 "notepad.exe" processes.

How to make a script that worked in one administrator account, check all the users logged on and terminate duplicate process "notepad.exe" launch by the current user?

it is possible to make?
 
I have a code to found all logged users on server, but how integrated in "Gates" code?

my code:

--------------------------------------------------------
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 10")

If colSessions.Count = 0 Then
Wscript.Echo "No interactive logged users found"
Else
For Each objSession in colSessions

Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objItem in colList
WScript.Echo "Username: " & objItem.Name
Next
Next
End If
 
i thought Geates code did show all the other users notepad.exe sessions? sorry little rusty on terminal services, are you saying that Geate;s code does not show all the other terminal sessions users notepads?
 
If I used Geates code I must run it at all loged users.

If my server is logged in 10 users, will be running 10 processes.

I need to run one script (1 process) that checks for all users logged on and remove duplicate "notepad.exe" process on current user.
 
Only check for the oldest process for the current user. Terminate everyone else's.
Code:
for each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)
    if (strOwner = strUserName) then
        if (convertDate(objProcess.CreationDate) > datOldest) then
            dicProcesses.add objProcess.CreationDate, objProcess
        else
            datOldest = convertDate(objProcess.CreationDate)
        end if
    [blue]else
        dicProcesses.add objProcess.CreationDate, objProcess[/blue]
    end if
next

-Geates
 
I have a terminal server on which users logon.
For example:

users process
User1 notapad.exe
notepad.exe

user2 notepad.exe

User3 notepad.exe
notepad.exe

user4 notepad.exe

user5 notepad.exe

Now I run the script, and it removes duplicate processes
For example:

User1 notapad.exe
notepad.exe (process terminated)

user2 notepad.exe

User3 notepad.exe
notepad.exe (process terminated)

user4 notepad.exe

user5 notepad.exe
 
Hi,

The following code finds a duplicate process the user's accoun, but it does not terminate why?

Code:
Dim strUserName

Set objWMI = GetObject("winmgmts:" _ 
              & "{impersonationLevel=impersonate}!\\" _ 
              & strComputer & "\root\cimv2") 


Set colSessions = objWMI.ExecQuery _ 
    ("Select * from Win32_LogonSession Where LogonType = 10")

set objNetwork = CreateObject("WScript.Network")

set colProcesses = objWMI.ExecQuery("Select * from Win32_Process Where Name = 'Notepad.exe'")
set dicProcesses = CreateObject("Scripting.Dictionary")

function convertDate(dtmDate)

    convertDate = cdate(mid(dtmDate, 5, 2) & "/" & mid(dtmDate, 7, 2) & "/" & left(dtmDate, 4) & " " & mid (dtmDate, 9, 2) & ":" & mid

(dtmDate, 11, 2) & ":" & mid(dtmDate,13, 2))

end function


If colSessions.Count = 0 Then 
   Wscript.Echo "No interactive users logged found"
 
Else 

   For Each objSession in colSessions 

     Set colList = objWMI.ExecQuery("Associators of " _ 
         & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _ 
         & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) 
     For Each objItem in colList 
       strUserName = objItem.Name

datOldest = now

for each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)

    if (strOwner = strUserName) then
        if (convertDate(objProcess.CreationDate) > datOldest) then
            dicProcesses.add objProcess.CreationDate, objProcess

        else
            datOldest = convertDate(objProcess.CreationDate)
        end if
    end if

next

for each objProcess in ColProcesses

if (dicProcesses.Exists(objProcess.CreationDate)) = 0 then

else

  objProcess.Terminate()

End if

Next
     Next 
   Next 
End If
 
im not sure i can comment on why exactly but perhaps i can comment on why you dont know why.

put some logging into your script, simple Wscript.Echo "in loop with colProcesses dictionary " & objProcess.SomeProperty" Wscript.Echo "colProcesses.Count=" & CStr(colProcesses.Count)
perhaps trap the return code from the .Terminate() method (if it is of any use)

these steps may lead you to understanding why it is not behaving as you expect
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top