#185518 - 2008-02-20 09:12 PM
Retreiving Username logged in from workstation hoast name
|
Indigoseth
Getting the hang of it
Registered: 2007-11-16
Posts: 78
|
I need to figure out who is logged in based on the workstation name they are logged into.
Anyone know of a UDF that will do this?
Thanks
Indigo
|
|
Top
|
|
|
|
#185524 - 2008-02-21 01:55 AM
Re: Retreiving Username logged in from workstation hoast name
[Re: Indigoseth]
|
lukeod
Getting the hang of it
Registered: 2008-01-11
Posts: 70
Loc: Australia
|
Not sure exactly what you mean - but i know that server 2003 has an inbuilt 'whos logged onto what' (right click my computer --> Manage --> System Tools --> Shared Folders --> Sessions.
It may be possible to pull back this info via WMI or somthing? Not sure if it's possible, but it may be worth seeing if system tools are included in WMI.
Oh, only really works on the Domain Controller as far as i know.
EDIT:
Just found this http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0216.mspx
if you were to find the equivilant in KIX and pointed the command @ the domain controller, i think it'd work.
Also, running the command "Net Sessions" from command prompt on the domain controller will list all open sessions. I recall seeing some sort of UDF to get output for commands like this and putting it into an array. I also recall seeing a UDF to run a command as if it were being run on a remote host - smiliar to TELNET, so you could script this to be run remotley if you have sufficent priviliges on the server.
All depends on what you want it for - just displaying it or using the output in code, the description wasn't very... descriptive!
Edited by lukeod (2008-02-21 02:05 AM)
|
|
Top
|
|
|
|
#185547 - 2008-02-21 11:51 AM
Re: Retreiving Username logged in from workstation hoast name
[Re: Mart]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is a sample that you can turn into a UDF:
Break ON
; WHOSON.KIX: Display user account(s) logged on to a machine ; ; Author: Richard Howarth <rhowarth@*obscured*> ; ; Description: ; Show who's logged on to a machine ; ; Command line parameters: ; $HOST Computer to view, default is this computer.
$=SetOption("Explicit","ON") $=SetOption("WrapAtEOL","ON") $=SetOption("ASCII","ON")
Dim $oWMIService Dim $oProcess,$colProcess Dim $sProcessUser, $sProcessDomain
If Not IsDeclared($Host) Global $Host EndIf
If $Host="" $Host=@WKSTA EndIf
$oWMIService = GetObject("winmgmts:\\"+$Host+"\root\cimv2") If @ERROR "Cannot access WMI sevrice, error is " @ERROR ": " @SERROR ? Exit @ERROR EndIf $colProcess=$oWMIService.ExecQuery('Select * from Win32_Process where Name="explorer.exe"',,48) If @ERROR "Cannot access WMI sevrice, error is " @ERROR ": " @SERROR ? Exit @ERROR EndIf For Each $oProcess In $colProcess $sProcessDomain = $oProcess.ExecMethod_("GetOwner").Domain $sProcessUser = $oProcess.ExecMethod_("GetOwner").User $sProcessUser=$sProcessDomain+"\"+$sProcessUser If $sProcessUser="\" $sProcessUser="[unknown]" EndIf "User logged on to "+$HOST+": "+$sProcessUser+@CRLF Next
Exit 0
|
A couple of notes:
- By default the script runs against your own computer
- To run against another computer, specify $HOST=TargetComputer on the command line
- If you are going to run against another computer you will of course need sufficient privilege
- Don't be surprised it multiple names are listed as being logged in, especially of you connect to a machine running terminal services
 - Likewise if you run against a Citrix server which is not using published desktops, don't be surprised if it doesn't display any logged on users
|
|
Top
|
|
|
|
#185574 - 2008-02-21 06:34 PM
Re: Retreiving Username logged in from workstation hoast name
[Re: Richard H.]
|
Indigoseth
Getting the hang of it
Registered: 2007-11-16
Posts: 78
|
I found a UDF that does a great job.
fnWMILoggedin
;FUNCTION fnWMILoggedIn()
;
;ACTION Retrieves users/computers connected to a computer
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.2 (changed array redim method)
; 1.1
;
;DATE CREATED 2002/12/05
;
;DATE MODIFIED 2003/05/26
;
;KIXTART 4.20+
;
;SYNTAX fnWMILoggedIn([$iMode, $sComputerName , $sUsername, $sPassword])
;
;PARAMETERS $IMODE
; Optional bit-wise integer denoting which users to retrieve
; 1 - Interactive users
; 2 - All local non-interactive users (service accounts)
; 4 - All remote users connected to local resources
; 8 - All remote computers connected to local resources
;
; $SCOMPUTERNAME
; Optional string containing the target computers name, defaults to local computer
;
; $SUSERNAME
; optional username when connection to a remote system
;
; $SPASSWORD
; optional password when connecting to a remote system
;
;RETURNS string or array of usernames
;
;REMARKS If $iMode is omitted only the interactive user is being returned
;
;DEPENDENCIES WMI, ADSI
;
;EXAMPLE: $rc=fnWMILoggedIn()
; $rc=fnWMILoggedIn(3,'workstation')
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000337
;
Function fnWMILoggedin(optional $iMode, optional $sComputerName, optional $sUserName, optional $sPassword)
dim $objLocator, $objWBEM, $sUserNames, $sNameSpace, $sName
dim $objAdsFso, $objSystem, $sCompName, $objAdsSession
$fnWMILoggedin=''
$iMode=val($iMode)
$sNameSpace = 'root\CIMV2'
; check to see whether we're connecting to a local or remote computer
$sComputerName=trim($sComputerName)
select
case $sComputerName=@WKSTA
$sCompName=$sComputerName
$sComputerName='.'
case $sComputerName
$sCompName=$sComputerName
case 1
$sCompName=@WKSTA
$sComputerName='.'
endselect
select
case $sUserName and $sComputerName<>'.'
; create locator object for connection to a remote computer
$objLocator = CreateObject('WbemScripting.SWbemLocator')
if @ERROR
exit @ERROR
endif
; create an credentialed (username/password provided) connection to a remote computer
$objWBEM=$objLocator.ConnectServer($sComputerName,$sNameSpace,$sUserName,$sPassword)
if @ERROR
exit @ERROR
endif
; set the impersonation level
$objWBEM.Security_.ImpersonationLevel = 3
if @ERROR
exit @ERROR
endif
case 1
;set the impersonation level
$objWBEM=GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$sComputerName+'\'+$sNameSpace)
if @ERROR
exit @ERROR
endif
endselect
if $iMode=0 or ($iMode & 1)
$objSystem = $objWBEM.ExecQuery('Select UserName from Win32_ComputerSystem')
for each $sName in $objSystem
if $sName.UserName
redim preserve $sUserNames[ubound($sUserNames)+1]
$sUserNames[ubound($sUserNames)] = $sName.UserName
endif
next
endif
if ($iMode & 2)
$objSystem = $objWBEM.ExecQuery("Select StartName from Win32_Service WHERE State='Running' AND StartName<>'LocalSystem'")
for each $sName in $objSystem
redim preserve $sUserNames[ubound($sUserNames)+1]
if left($sName.StartName,2)='.\'
if $sCompName
$sUserNames[ubound($sUserNames)] = ucase($sCompName)+substr($sName.StartName,2)
else
$sUserNames[ubound($sUserNames)] = ucase(@WKSTA)+substr($sName.StartName,2)
endif
else
$sUserNames[ubound($sUserNames)] = $sName.StartName
endif
next
endif
if ($iMode & 4) or ($iMode & 8)
$objAdsFSO = GetObject('WinNT://'+$sCompName+'/LanmanServer')
for each $objAdsSession In $objAdsFSO.Sessions
$sName=$objAdsSession.User
select
case $sName=''
; inter-server connection
case right($sName,1)='$' and ($iMode & 8) and ascan($sUserNames,$sName)=-1
redim preserve $sUserNames[ubound($sUserNames)+1]
$sUserNames[ubound($sUserNames)]=ucase(left($sName,len($sName)-1))
case right($sName,1)<>'$' and ($iMode & 4) and ascan($sUserNames,$sName)=-1
redim preserve $sUserNames[ubound($sUserNames)+1]
if not instr($sName,'\')
$sUserNames[ubound($sUserNames)]=$sCompName+'\'+$sName
else
$sUserNames[ubound($sUserNames)]=$sName
endif
endselect
Next
$objAdsFSO = 0
endif
$objWBEM=0
$objLocator=0
if ubound($sUserNames)=0
$sUserNames=$sUserNames[0]
endif
$fnWMILoggedin=$sUserNames
exit @ERROR
EndFunction
It returns domain\usrname.
I did a substring and it pulled out the name, I also did a trim to pull out any spaces on the outside that may be left.
I was then left with the user name.
|
|
Top
|
|
|
|
#185575 - 2008-02-21 06:35 PM
Re: Retreiving Username logged in from workstation hoast name
[Re: Indigoseth]
|
Indigoseth
Getting the hang of it
Registered: 2007-11-16
Posts: 78
|
I am fighting another problem though... I am trying to send a log file via email.. Anyone know of a emailer that works good with Kix shell?
Indigo
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1058 anonymous users online.
|
|
|