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