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

Miscellaneous

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

Currently Logged on User on Specified Machine

 Keywords:  Currently Logged on in Specific Machine Computer Remote Current User

Question:

Is there an function that will retrieve the currently logged on user given a valid windows workstation name?

Answer:

Sorry no built in function. However, here is some code that can be used to find out who was logged on to a remote NT4 workstation. This code uses ipPing to see if the workstation is accessible.

This is based on the idea that when someone is logged on to a workstation, HKEY_USERS has two subkeys - ".default" and the SID of the logged on user. I use the SID to look up the "profileimagepath" in the registry. This is the path to the user-specific files. I extract the last part of that path which is the user's ID.

IntControl(12,5,0,0,0)
AddExtender("WWWNT34I.DLL")
AddExtender("WWIPG34I.DLL")

;-----------------------------------------------------------------------------------
:init
reg_sz=1
reg_expand_sz=2
reg_binary=3
reg_dword=4
reg_multi_sz=7


;-----------------------------------------------------------------------------------
:main
pcname = "FRED" ; Note: Don't use slashes

;-----------------------------------------------------------------------------------
:ping
a=ipPing(pcname,15)
If a == @TRUE Then Goto goodping
msg = "Ping unsuccessful"
Goto main

;-----------------------------------------------------------------------------------
:goodping

;connect to the registry of the workstation being queried
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)
Goto main

;-----------------------------------------------------------------------------------
:endall

Article ID:   W15535
File Created: 2003:05:13:11:28:36
Last Updated: 2003:05:13:11:28:36