WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Remote Process UDFs

 Keywords:  remote process process list run launch kill terminate close


#DefineFunction ListRemoteProcesses(computername,username,password)
;***************************************************************************
;** ListRemoteProcesses(computername,username,password)
;**  Lists processes on a remote computer.
;** 
;** Parameters:
;**  ComputerName:Computer name without \\, and "" for local.
;**
;**  Note that root/cimv2 is a namespace. For example, within the \root namespace are two instances of namespace. 
;**  One is "Default", and the other is "Cimv2." These instances represent the \root\default, and \root\cimv2 namespaces, 
;**  respectively. In other words, this shouldn't be changed.
;**
;**  UserName:		User name to run program as. In format DOMAIN\User or just User for local, "" for  
;**               current user running script. MUST be "" for local computer or the function will fail.
;**
;**  Password: 	Password for user or "" if username is "".
;** 
;** Returns:
;** 					a tab delimited list of process names and IDs.
;**					The return value, 'process list' is returned in the following format:
;**					[processname|processid]@TAB[processname|processid]
;**
;**
;***************************************************************************
	list = ""
	Locator = ObjectOpen("WbemScripting.SWbemLocator")
	Service = Locator.ConnectServer(computername,"root/cimv2",username,password)
	Security = Service.Security_
	Security.ImpersonationLevel = 3
	Instance = Service.InstancesOf("win32_process")
	hEnum = ObjectCollectionOpen(Instance)
	
	While 1
	  Obj = ObjectCollectionNext(hEnum)
	  If Obj == 0 Then Break
	  list = StrCat(list,@tab,Obj.name,"|",Obj.processid)
	EndWhile
	
	list = StrTrim(list)
	
	ObjectCollectionClose(hEnum)
	ObjectClose(Instance)
	ObjectClose(Security)
	ObjectClose(Service)
	ObjectClose(Locator)
	
	Return list
#EndFunction



#DefineFunction KillRemoteProcess(computername, username, password, processname_id, type)
;***************************************************************************
;** KillRemoteProcess(computername, username, password, processname_id, type)
;**  Kills a process on a remote computer.
;** 
;** Parameters:
;**  ComputerName:	Computer name without \\, and "" for local.
;**  UserName:		User name to run program as. In format DOMAIN\User or just User for local, "" for  
;**					current user running script. MUST be "" for local computer or the function will fail.
;**  Password: 		Password for user or "" if username is "".
;**  processname_id: process name or process id 
;**  type: 			0-process name passed, 1-process id passed
;** 
;** Returns:
;** 					1-success, 0-failure.
;**
;**
;***************************************************************************
	Locator = ObjectOpen("WbemScripting.SWbemLocator")
	Service = Locator.ConnectServer(computername,"root/cimv2",username,password)
	Security = Service.Security_
	Security.ImpersonationLevel = 3
	Instance = Service.InstancesOf("win32_process")
	hEnum = ObjectCollectionOpen(Instance)

	if type !=0 and type !=1
       Message("KillRemoteProcess Error","invalid type specified")
		 return @false
	endif
	
	if type == 0
		While 1
		  Obj = ObjectCollectionNext(hEnum)
		  if Obj == 0 Then Break
		  if StrLower(Obj.name)==StrLower(processname_id)
			  Obj.Terminate
		  endif
		EndWhile
	else
		if type == 1
			While 1
			  Obj = ObjectCollectionNext(hEnum)
			  if Obj == 0 Then Break
			  if Obj.Processid==processname_id
				  Obj.Terminate
			  endif
			EndWhile
		 else
			 Message("KillRemoteProcess Error","invalid type specified")
			 return @false
		 endif
	endif

	ObjectCollectionClose(hEnum)
	ObjectClose(Instance)
	ObjectClose(Security)
	ObjectClose(Service)
	ObjectClose(Locator)
	Return @true
#EndFunction


#DefineFunction RunRemoteProcess(ComputerName,Application,WorkingDirectory,User,Password)
;***************************************************************************
;** RunRemoteProcess(ComputerName,Application,WorkingDir,UserName,Password)
;** Runs an application on a remote computer.
;** 
;** Parameters:
;** ComputerName: Computer name without \\, and "" for local.
;** Application: Program to run. Can include full path or UNC. Can also include Parameters.
;** WorkingDir: Working Directory for program or "" for default.
;** UserName: User name to run program as
;** In format DOMAIN\User or just User for local, "" for current user running script.
;** MUST be "" for local computer or the function will fail.
;** Password: Password for user or "" if username is "".
;** 
;** Returns:
;** Result: the result of the create call.
;** Value Meaning 
;** 0 Successful completion 
;** 2 Access denied 
;** 3 Insufficient privilege 
;** 8 Unknown failure 
;** 9 Path not found 
;** 21 Invalid parameter 
;**
;** !!!!!!! NOTES !!!!!!!!!
;** If the newly created process attempts to interact with objects on the target system without 
;** appropriate access privileges, it is terminated without notification to this method.
;**
;** Note  The Win32_Process.Create method cannot be used to start an interactive process for security reasons.
;**
;***************************************************************************
	Locator = ObjectOpen("WbemScripting.SWbemLocator")
	Service = Locator.ConnectServer(ComputerName,"root/cimv2",User,Password)
	Security = Service.Security_
	Security.ImpersonationLevel = 3
	Class = Service.Get("Win32_Process")
	If WorkingDirectory == "" then
		Result = Class.Create(Application)  ;no working directory specified
	Else
		Result = Class.Create(Application,WorkingDirectory) ;pass the specified working directory
	EndIf
	ObjectClose(Class)
	ObjectClose(Security)
	ObjectClose(Service)
	ObjectClose(Locator)
	Return Result
#EndFunction


;***************************************************************************
;**     Main
;***************************************************************************
computername="SCOOBY"
username = ""
password = ""
workdir = ""
application = "notepad.exe"

ret = AskYesNo("RunRemoteProcess","Would you like to run %application% on %computername%?")
if ret == @yes then RunRemoteProcess(computername,application,workdir,username,password)

list = ListRemoteProcesses(computername,username,password)
processinfo = AskItemList("Choose Remote Process to Kill",list,@tab,@sorted,@single)
procid = ItemExtract(2,processinfo,"|")
KillRemoteProcess(computername,username,password,procid,1)

Message("","Done")
exit


Article ID:   W15790
File Created: 2003:05:13:11:30:06
Last Updated: 2003:05:13:11:30:06