Page 1 of 2 12>
Topic Options
#150009 - 2005-10-15 12:54 AM Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
I have a scheduled maintenance script that runs as a service account. It runs at logon and every hour. I would like it to know who the logged-in user is on the workstation. Anybody have a good way to do this???

My current method is for the logon script to simply do a writeprofilestring("userinfo.ini","UserInfo","UserID",@USERID) to a known location and then have the maintenance script pick this up. But I have some timing issues at logon where the maintenance script is firing off faster than the user's logon script and picking up the PREVIOUS logged in user.

I'm thinking I need a different way of doing this. I seem to remember some command (maybe in the resource kit) which will list all the terminal and console login sessions. I could parse this I thought.

Ideas?

Top
#150010 - 2005-10-15 01:17 AM Re: Best way to detect the logged-in user.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
winnt-provider has a sessions kinda thing...
dunno if it works only for server but...

and ofcourse you can check for the loaded user hives.
something like:
for each $key in $HKEY_USERS_KEYS

as only the logged in user's key is loaded.
_________________________
!

download KiXnet

Top
#150011 - 2005-10-15 01:57 AM Re: Best way to detect the logged-in user.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
There are some UDFs to check who is logged on remotely.

Sessions on a Terminal Server is not the same as a locally logged on user.

Top
#150012 - 2005-10-15 02:16 AM Re: Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
Quote:

and ofcourse you can check for the loaded user hives.
something like:
for each $key in $HKEY_USERS_KEYS

as only the logged in user's key is loaded.




Hmmm, I'm not familiar with how you check which hive is loaded... Do I compare something in there with a value in HKCU???

Also, how can I tell the difference between an account logged on locally vs a service account (like the one my script is running as)??

Thanks for the suggestions.

Top
#150013 - 2005-10-15 04:44 AM Re: Best way to detect the logged-in user.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
This is discussed on my web page.

http://home.comcast.net/~habullock/kix_solutions.htm

The solution provided uses my Win32Admin.DLL version 3. The script also uses IFF so you will need to use a version of KiXtart that support IIF. I also hard coded my computer name in the script. You will have to remove or change it.
Code:
break on


$Win32Admin = createobject("Win32Admin")
if vartypename($Win32Admin) <> "Object"
? "@serror"
endif

; Requires KiXtart 4.02 or higher
; You can define the $Computer variable here or an the command line.
;
; Syntax: Kix32 ListLoggedOnUsers.kix $Computer="Name"
;
; Note that if your computer name is like "1 something" the command line
; option may not work
;
If VarTypeName($Computer) = 'Empty'
$Computer = ""
Endif
$Computer = "bullockha3"

;Create VBS Dictionary Object
$oDict = CreateObject("Scripting.Dictionary")


; Method EnumTrustedDomains(optional $Server)
; Returns an array of dictionary objects
; Keys: (sid, name)
; If a server name is specified, the enumeration is based
; on the domain where the server resides. If no server is supplied,
; then the computer where the script is executing will be used by
; default.
$Domains = $Win32Admin.EnumTrustedDomains($Win32Admin.GetAnyDC(@domain, @wksta))
if @error = 0
for each $Domain in $Domains
$oDict.Add($Domain.sid, $Domain.name)

;uncomment to see the values
;---------------------------
;Dim $keys, $Value
;$keys = $Domain.keys
;for each $key in $keys
; $Value = $Domain.get($key)
; ? $key + " = " + $Value
;next
;?
next
else
? "Error: @error @serror"
endif



For Each $key In fEnumKey($Computer, "HKEY_USERS")
Dim $acct
If Left($key,3) = "S-1" and Instr($key,"_") = 0
$DomainSid = Left($key,instrrev($key,"-")-1)

$Info = $Win32Admin.LsaLookupSids ($Computer, $key)
if @error = 0
for each $item in $Info
;uncomment to see the values
;---------------------------
;$keys = $item.keys
;for each $key in $keys
; $Value = $item.get($key)
; ? $key + " = " + $Value
;next
;?

; Note: Be careful here as if you pass more than one SID to
; LsaLookupSids you will get back more than $item.
$Account = $item.name
$Domain = $oDict.Item($DomainSid)
? "" + $Domain + IIF($domain, "\", "") + $Account
next
else
? "Error: @error @serror"
endif


; $acct = SidToName($key)
; If $acct = ''
; $acct = 'Sid from unknown SAM database.'
; Endif
; ? $key + " - " + $acct
Endif
Next
exit 0




;FUNCTION fEnumKey($Computer, $Key)
;
;AUTHOR Howard A. Bullock (habullock@comcast.net)
;
;ACTION Enumerates registries keys on the specified computer.
;
;SYNTAX fEnumKey($Computer, $Key)
;
;PARAMETERS $Computer (Required) - String value
; $Key (Required) - String value
;
;REMARKS Do not prefix the computer name with "\\"'s.
;
;RETURNS Array of keys names.
;
;DEPENDENCIES KiXtart 4.02
;
;EXAMPLES fEnumKey("", "HKEY_USERS")
; fEnumKey("Remote1", "HKEY_USERS")
;
Function fEnumKey($Server, $Key)
Dim $Index, $Error
$Index = 0
Dim $KeyName[$Index]
If $Server <> ""
$Key = "\\" + $Server + "\" + $Key
Endif

If KeyExist($Key)
Do
$KeyName[$Index] = ENUMKEY($Key, $Index)
$Error = @Error
If NOT $Error
$Index = $Index + 1
ReDim PRESERVE $KeyName[$Index]
Else
$Index = $Index - 1
ReDim PRESERVE $KeyName[$Index]
Endif
Until $Error
Else
$KeyName[0] = ""
Exit 2
Endif
$fEnumKey = $KeyName
Exit 0
Endfunction

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#150014 - 2005-10-15 03:52 PM Re: Best way to detect the logged-in user.
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
There's also fnWMILoggedIn() - Retrieves users/computers connected to a computer for both local/remote user lists but but it was not reliable for me. I prefer to use the NETUSERS utility... works every time.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#150015 - 2005-10-17 11:56 AM Re: Best way to detect the logged-in user.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you want to know who is logged on to the machine, why not just enumerate your local processes?

If you ignore all the daemon process owners what's left will be the user(s).

If you run full desktops it's even easier - you just need to look for any "explorer.exe" processes.

Top
#150016 - 2005-10-17 07:01 PM Re: Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
Quote:

If you want to know who is logged on to the machine, why not just enumerate your local processes?

If you ignore all the daemon process owners what's left will be the user(s).

If you run full desktops it's even easier - you just need to look for any "explorer.exe" processes.




I like that idea. So pardon the question, but what is the best way to enumerate all explorer.exe's and grab the user? I've done a search and see an example using cmdow.exe but I;d like to avoid any outside executibles if I can.

tasklist /FI would work, but I dont get the user name on that.

Top
#150017 - 2005-10-17 07:26 PM Re: Best way to detect the logged-in user.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$computer = 'computername'

$id = WMIQuery('CSName', 'Win32_Process', $computer ,'Description','explorer.exe')

for each $proc in $id
? $proc
next

using the WMIQuery() UDF


Edited by Radimus (2005-10-17 07:27 PM)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150018 - 2005-10-17 09:13 PM Re: Best way to detect the logged-in user.
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
'CSName' isn't the name of the user, but rather the name of the computer running the process.
Top
#150019 - 2005-10-17 09:13 PM Re: Best way to detect the logged-in user.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
KiXtart doesn't support the OUT parameter of .GetOwner on the Win32_Process query.

The example here in VBScript works great, but KiXtart doesn't support that paramter. I've posted in the Suggestions to see if it's possible to be added to KiXtart in the future.

http://msdn.microsoft.com/library/defaul...n32_process.asp

 

Top
#150020 - 2005-10-17 09:23 PM Re: Best way to detect the logged-in user.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
dammit
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150021 - 2005-10-17 11:20 PM Re: Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
So where does that leave me??? If I can't grab the name of the user running the process that method wont work. Hmm...
Top
#150022 - 2005-10-17 11:49 PM Re: Best way to detect the logged-in user.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$id = WMIQuery('Name','Win32_ComputerSystem',$computer)

for each $proc in $id
? $proc
next
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150023 - 2005-10-18 01:02 AM Re: Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
Quote:

$id = WMIQuery('Name','Win32_ComputerSystem',$computer)

for each $proc in $id
? $proc
next




What is that supposed to do? When I run it, it returns my workstation name.

Top
#150024 - 2005-10-18 01:24 AM Re: Best way to detect the logged-in user.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
what value are you giving $computer
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150025 - 2005-10-18 01:35 AM Re: Best way to detect the logged-in user.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Rad, that won't tell him what users are logged on. It will tell him what processes are running but that's not exactly what he wants. You could do both. shell out and run the VBScript portion of the code and then feed it to wshpipe or a file and read it back into KiX.
Top
#150026 - 2005-10-18 01:39 AM Re: Best way to detect the logged-in user.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
also...

Quote:

UserName
Data type: string
Access type: Read-only

Name of a user that is logged on currently. This property must have a value. In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session.




Top
#150027 - 2005-10-18 01:51 AM Re: Best way to detect the logged-in user.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Quote:

Quote:

$id = WMIQuery('Name','Win32_ComputerSystem',$computer)

for each $proc in $id
? $proc
next




What is that supposed to do? When I run it, it returns my workstation name.




This works just fine... It is what we use in SIM
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#150028 - 2005-10-18 02:08 AM Re: Best way to detect the logged-in user.
sixdoubleo Offline
Starting to like KiXtart

Registered: 2004-02-06
Posts: 118
Loc: California, US
Quote:

what value are you giving $computer




$computer = @WKSTA

Top
Page 1 of 2 12>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 759 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.108 seconds in which 0.057 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org