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

Samples from Users
plus
plus
plus
plus
plus
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.

Determine Current Interactive User From System Account


Developer: ferchl

Often, while running a script as the System account on Windows 2K and XP workstations, I need to know the user name, domain, and SID of the current interactively logged in user. Although there is a WMI call to do this on XP, that call does not work on W2K if the user is a non-admin. In the next message is my solution for determining the current interactively logged in user. It uses no WMI calls or extenders. I thought this might be useful to others.

#DefineFunction CurrentInteractiveUser()
UserName = ""
UserDomain = ""
UserSID = ""
WinlogonKey = "Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
GINAUser = RegQueryValue(@REGMACHINE,WinlogonKey:"[DefaultUserName]")
UserDomain = RegQueryValue(@REGMACHINE,WinlogonKey:"[DefaultDomainName]")
Key = RegOpenKey(@REGUSERS,"")
SIDList = RegQueryKeys(Key)
RegCloseKey(Key)
For i = 1 To ItemCount(SIDList,@TAB)
   UserSID = ItemExtract(i,SIDList,@TAB)
   If StrSub(UserSID,1,8)=="S-1-5-21" && StrIndexNC(UserSID,"_Classes",1,@FWDSCAN)==0
      KeyVal = UserSID:"\Software\Microsoft\Windows\CurrentVersion\Explorer[Logon User Name]"
      If RegExistValue(@REGUSERS,KeyVal) Then UserName = RegQueryValue(@REGUSERS,KeyVal)
      If StrUpper(UserName)==StrUpper(GINAUser) Then Break
   EndIf
Next
Return UserName:@TAB:UserDomain:@TAB:UserSID
#EndFunction

; Test
UserInfo = CurrentInteractiveUser()
UserName = ItemExtract(1,UserInfo,@TAB)
UserDomain = ItemExtract(2,UserInfo,@TAB)
UserSID = ItemExtract(3,UserInfo,@TAB)
Message("Current Interactive User:",UserName:@CRLF:UserDomain:@CRLF:UserSID)

  

Article ID:   W17465
File Created: 2008:04:10:15:11:00
Last Updated: 2008:04:10:15:11:00