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

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

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

Get Current User Remotely

 Keywords:  

Question:

Is there a function to get the current user on a workstation remotely?

Answer:

If you're using NT this should tell you who is logged in:
errormode(@off)
hnd = RegConnect("\\%pcname%",@regusers)
errormode(@cancel)

hnd2=RegOpenKey(hnd,"")
userlist = regquerykeys(hnd2)
numusers = itemcount(userlist,@tab)
RegCloseKey(hnd)

errormode(@off)
hnd = RegConnect("\\%pcname%",@regmachine)
errormode(@cancel)

if numusers > 1
	loggedonuser = itemextract(2,userlist,@tab)
	hnd2 = regopenkey(hnd,"SOFTWARE\Microsoft\Windows
	NT\CurrentVersion\ProfileList\%loggedonuser%")
	datatype = regentrytype(hnd2,"[profileimagepath]")
	path = regqueryex(hnd2,"[profileimagepath]",@tab,datatype)
	pathlen = strlen(path)
	backslash = strindex(path,"\",0,@backscan)
	idlen = pathlen - backslash
	msg = strsub(path,backslash + 1,idlen)
else
	msg = "None"
endif

errormode(@off)
WNTCancelCon("\\%pcname%\ipc$",@TRUE,@TRUE)
errormode(@cancel)

You will have to provide the "pcname". The result is placed in "msg".

I combined this with a dialog so I could check any of thousands of remote workstations to see who, if anyone, is logged on.


PsLoggedOn v1.34By Mark Russinovich

Introduction
You can determine who is using resources on your local computer with the "net" command ("net session"), however, there is no built-in way to determine who is using the resources of a remote computer. In addition, NT comes with no tools to see who is logged onto a computer, either locally or remotely. PsLoggedOn is an applet that displays both the locally logged on users and users logged on via resources for either the local computer, or a remote one. If you specify a user name instead of a computer, PsLoggedOn searches the computers in the network neighborhood and tells you if the user is currently logged on.

PsLoggedOn's definition of a locally logged on user is one that has their profile loaded into the Registry, so PsLoggedOn determines who is logged on by scanning the keys under the HKEY_USERS key. For each key that has a name that is a user SID (security Identifier), PsLoggedOn looks up the corresponding user name and displays it. To determine who is logged onto a computer via resource shares, PsLoggedOn uses the NetSessionEnum API. Note that PsLoggedOn will show you as logged on via resource share to remote computers that you query because a logon is required for PsLoggedOn to access the Registry of a remote system.

computername = ComputerNameGet( 0 ) ;Modify to fit your needs
outputfile = 'c:\PSLoggedon.txt'
If FileExist( outputfile ) Then FileDelete( outputfile )
pslogon = 'D:\Tools\PsTools\PsLoggedon.exe'
path = FilePath(pslogon)
DirChange(path)
commandline = '/c "':pslogon:'" -l -x \\' : computername : '>' :  outputfile
cmd =  Environment('COMSPEC')
RunShell(cmd, commandline, path,  @NORMAL, @WAIT)
If FileExist(outputfile)
   data = FileGet( outputfile )
   Pause('PSLoggedon Results',data)
Else
   Pause('Notice','Unable to locate file generated from PSLoggedon!')
EndIf

Article ID:   W15513
File Created: 2011:12:06:14:23:42
Last Updated: 2011:12:06:14:23:42