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.

Get Owner of a Process


This code retrieves the user name and domain name under which the process is running.

The GetOwner method retruns two output parameters. You must pass binary buffers that have been typed using the BinaryOleType function. Then retrieve the output from the binary buffer.

sSiteSrv = "scooby"
oWMILocator = ObjectOpen("WbemScripting.SWbemLocator")

; Set authentication and impersonation levels for WMI access.
oWMISecurity = oWMILocator.Security_
oWMISecurity.AuthenticationLevel = 3
oWMISecurity.ImpersonationLevel = 3

ErrorMode(@off)
oWMIServices = oWMILocator.ConnectServer(sSiteSrv, "root/Cimv2", "", "")
ErrorMode(@cancel)
If LastError() <> 0
	; WMI Access denied.
	Message("Error", "Unable to connect to the remote server.%@CRLF%%@CRLF%The server may be unavailable or your ID may have insufficient rights.%@CRLF%[%LastError%]", xHex(40))
	Exit
EndIf

; Sample: Get a list of SMS providers on the remote system.
colSMSProvider = oWMIServices.ExecQuery("Select Name, SessionId from Win32_Process where SessionId = '0' and Name = 'Explorer.exe'", "WQL", 0)

oColSystem = ObjectCollectionOpen(colSMSProvider)

While 1
	bufNameOfUser = BinaryAlloc(256) 
	bufUserDomain = BinaryAlloc(256) 
	BinaryOLEType(bufNameOfUser,202,0,0,0)
	BinaryOLEType(bufUserDomain,202,0,0,0)
	
	oCurSystem = ObjectCollectionNext(oColSystem)
	If oCurSystem == 0 Then Break
	sProcessName = oCurSystem.Name
	sProcessId = oCurSystem.SessionId
	
	; Next line is a WMI output-only method
	colProperties = oCurSystem.GetOwner(bufNameOfUser,bufUserDomain)  
	
	strNameOfUser = BinaryPeekStr(bufNameOfUser, 0, BinaryEodGet(bufNameOfUser)) 
	strUserDomain = BinaryPeekStr(bufUserDomain, 0, BinaryEodGet(bufUserDomain)) 
	Message(sProcessName, StrCat("User: ",strNameOfUser,@crlf,"Domain: ",strUserDomain,@crlf,"SessionId: ",sProcessId))
	BinaryFree(bufNameOfUser) 
	BinaryFree(bufUserDomain) 
EndWhile


ObjectCollectionClose(oColSystem)

Article ID:   W16751
File Created: 2005:05:31:13:46:20
Last Updated: 2005:05:31:13:46:20