chadmanvb
asked on
get login time and how the user logged in with .net 2.0 visual basic
I have some code that get's the login time and how the user logged in, via console or remote access. This code works great unless someone users the runas command. Then it seems to give the login time on when the runas command was run. Anyway to fix that? Here is the code I'm currently using.
Private Function getWorkstationTypeTime(ByV al workstation As String) As String
Try
Dim strcomputer As String = workstation 'set strcomputer as workstation serial number
Dim WMIScope As Management.ManagementScope
Dim WMIConnectionOptions As New Management.ConnectionOptio ns
Dim strRootDomain As String
Dim objRootLDAP As DirectoryEntry
Dim query As ManagementObjectSearcher
Dim queryCollection As ManagementObjectCollection
Dim oq As New System.Management.ObjectQu ery
Dim mo As New ManagementObject
Dim perftimestamp As String = ""
Dim perttimefreq As String = ""
Dim counter As String = ""
Dim iUptimeInSec As Long = 0
With WMIConnectionOptions
.Impersonation = System.Management.Imperson ationLevel .Impersona te
.Authentication = System.Management.Authenti cationLeve l.Packet
End With
WMIScope = New Management.ManagementScope ("\\" & _
strcomputer & "\root\cimv2", WMIConnectionOptions)
objRootLDAP = New DirectoryEntry("LDAP://Roo tDSE")
strRootDomain = objRootLDAP.Properties.Ite m("rootDom ainNamingC ontext").V alue.ToStr ing
oq = New System.Management.ObjectQu ery("Selec t * from Win32_Session")
query = New ManagementObjectSearcher(W MIScope, oq)
queryCollection = query.Get()
Dim consoleLogin As Boolean = False
Dim remoteLogin As Boolean = False
Dim consoleTime As String = ""
Dim remoteTime As String = ""
Dim realTime As String = ""
For Each oReturn As ManagementObject In queryCollection
If oReturn("LogonType").ToStr ing().Equa ls("2") Then
' 2 - for logged on User
consoleLogin = True
Dim dmtfDate As String = oReturn("StartTime").ToStr ing
Dim returnValue As DateTime
returnValue = ManagementDateTimeConverte r.ToDateTi me(dmtfDat e)
consoleTime = (returnValue & ",Console Login")
Return consoleTime
End If
If oReturn("LogonType").ToStr ing().Equa ls("10") Then
remoteLogin = True
Dim dmtfDate As String = oReturn("StartTime").ToStr ing
Dim returnValue As DateTime
returnValue = ManagementDateTimeConverte r.ToDateTi me(dmtfDat e)
remoteTime = (returnValue & ",Remote Desktop Login")
End If
Next
Return remoteTime
Catch ex As Exception
Return ""
End Try
End Function
Private Function getWorkstationTypeTime(ByV
Try
Dim strcomputer As String = workstation 'set strcomputer as workstation serial number
Dim WMIScope As Management.ManagementScope
Dim WMIConnectionOptions As New Management.ConnectionOptio
Dim strRootDomain As String
Dim objRootLDAP As DirectoryEntry
Dim query As ManagementObjectSearcher
Dim queryCollection As ManagementObjectCollection
Dim oq As New System.Management.ObjectQu
Dim mo As New ManagementObject
Dim perftimestamp As String = ""
Dim perttimefreq As String = ""
Dim counter As String = ""
Dim iUptimeInSec As Long = 0
With WMIConnectionOptions
.Impersonation = System.Management.Imperson
.Authentication = System.Management.Authenti
End With
WMIScope = New Management.ManagementScope
strcomputer & "\root\cimv2", WMIConnectionOptions)
objRootLDAP = New DirectoryEntry("LDAP://Roo
strRootDomain = objRootLDAP.Properties.Ite
oq = New System.Management.ObjectQu
query = New ManagementObjectSearcher(W
queryCollection = query.Get()
Dim consoleLogin As Boolean = False
Dim remoteLogin As Boolean = False
Dim consoleTime As String = ""
Dim remoteTime As String = ""
Dim realTime As String = ""
For Each oReturn As ManagementObject In queryCollection
If oReturn("LogonType").ToStr
' 2 - for logged on User
consoleLogin = True
Dim dmtfDate As String = oReturn("StartTime").ToStr
Dim returnValue As DateTime
returnValue = ManagementDateTimeConverte
consoleTime = (returnValue & ",Console Login")
Return consoleTime
End If
If oReturn("LogonType").ToStr
remoteLogin = True
Dim dmtfDate As String = oReturn("StartTime").ToStr
Dim returnValue As DateTime
returnValue = ManagementDateTimeConverte
remoteTime = (returnValue & ",Remote Desktop Login")
End If
Next
Return remoteTime
Catch ex As Exception
Return ""
End Try
End Function
Perhaps you have already checked this, but just in case - your code seems to return the StartTime only from the last object in the session collection. Can you try to output all the sessions in the collection to see if the one you're looking for is in there?
ASKER
It does look like it does give the correct time, but it shows a bunch of id's/time until I see it. The id's are given with a session ID and I'm not sure what that means or how to convert that to the accual user id.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried that, but I think the syntax is wrong. Where would I put that line to get relatedQuery?
ASKER
Got it, works great. Thanks!