;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