#206045 - 2012-10-19 09:55 PM
Re: enum user session with WMI
[Re: ChristopheM]
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
What I go is have code in my logonscript to update a table in SQL DB with that info.
$cn = CreateObject("ADODB.Connection") $cmd = CreateObject("ADODB.Command") $rs = CreateObject("ADODB.RecordSet")
$cn.connectionstring = "DRIVER={SQL Server};SERVER=sqlserver;UID=UserName;PWD=password;DATABASE=invDB"
$cn.open $cmd.activeconnection = $cn $rs.cursortype = 3 $rs.locktype = 3
$rs.activecommand = $cmd
$cmdtxt = "select * from dbo._tbl_Main where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt
$rs.open($cmd)
If $rs.eof = -1 $rs.addnew EndIf
$rs.fields.item("UserName").value = @userid
$rs.update
Logfile(tab(3) + 'Updating User logon in SQL Inventory ' + @serror)
$rs.close
$cn.close
|
Top
|
|
|
|
#206047 - 2012-10-20 03:11 AM
Re: enum user session with WMI
[Re: ChristopheM]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4381
Loc: New Jersey
|
This is how I do it in my helpdesk app:; get the currently logged-on user, if any
If $SECURE $WMIOBJPTR = WMIAuthentication($txtHost.Text, $USERID , $USERPW) EndIf
$RC = ''
$ACTIVEUSERID = WMIQuery('UserName', 'Win32_ComputerSystem', $txtHost.Text, , , , $WMIOBJPTR)[0]
If InStr($ACTIVEUSERID, '\') $RC = Split($ACTIVEUSERID, '\')[1] Else $RC = $ACTIVEUSERID EndIf The two WMIxxxxx functions are available from the resources page of my website, and just slightly modified/updated versions of those posted on KORG. The above example was developed on XP and works on Vista/Win7.
$WMIOBJPTR can be null/eliminated unless you need a specific account to authenticate against the remote machine. WMIAuthentication() simply provides a standard function to get an authenticated WMI object rather than building it into every WMI function.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#206048 - 2012-10-20 03:57 PM
Re: enum user session with WMI
[Re: Glenn Barnas]
|
ChristopheM
Hey THIS is FUN
   
Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
|
thanks glenn
this is an other way but not really a question to my current problem. and i see another problem with this solution. according to MSDN site, the username property of win32_computersystem is set only for a console session not for a session with terminal service so sometime, i could have no information even if there is a user connected !!!
i had a look to your functions. your WMIAuthentication function is very near from my WMIConnectEx Except parameters, globally i just see one difference : your code :$objWBEM=GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$sComputerName+'\'+$sNameSpace) my code :$objWMIService=GetObject('winmgmts:{'+$SecuritySettings+'}!\\'+$strcomputer+'\'+$namespace) I use an optional parameter SecuritySettings where you have hardcoded "impersonationLevel=impersonate".
A word also about the WMIQuery function. No need to evaluate $sComputer and $root when $pAuth is defined because theses variables aren't used after (and evaluation has already be done by the call to WMIAuthentication). your code:
$sComputer = Trim(Join(Split($sComputer,'\'),''))
If Not $sComputer Or $sComputer = @WKSTA
$sComputer = '.'
EndIf
If Not $root
$root = '\root\cimv2'
Endif
If $pAuth
$SystemSet = $pAuth
Else
$SystemSet = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $sComputer + $root)
If @ERROR Or Not $SystemSet
Exit Val('&' + Right(DecToHex(@ERROR), 4))
EndIf
EndIf i suggest:
If $pAuth
$SystemSet = $pAuth
Else
$sComputer = Trim(Join(Split($sComputer,'\'),''))
If Not $sComputer Or $sComputer = @WKSTA
$sComputer = '.'
EndIf
If Not $root
$root = '\root\cimv2'
Endif
$SystemSet = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $sComputer + $root)
If @ERROR Or Not $SystemSet
Exit Val('&' + Right(DecToHex(@ERROR), 4))
EndIf
EndIf it is compatible with existing code and will execute a little faster when $pAuth is initialized.
_________________________
Christophe
|
Top
|
|
|
|
#206054 - 2012-10-22 09:04 PM
Re: enum user session with WMI
[Re: ChristopheM]
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
If the code is on the machine and is called via All Users startup, or in the local logon policy, the only ones not captured are machine the are logged into, but not connected to LAN at the time.
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 329 anonymous users online.
|
|
|