Link to home
Start Free TrialLog in
Avatar of chadmanvb
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(ByVal 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.ConnectionOptions
            Dim strRootDomain As String
            Dim objRootLDAP As DirectoryEntry
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
            Dim oq As New System.Management.ObjectQuery
            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.ImpersonationLevel.Impersonate
                .Authentication = System.Management.AuthenticationLevel.Packet

            End With

            WMIScope = New Management.ManagementScope("\\" & _
            strcomputer & "\root\cimv2", WMIConnectionOptions)

            objRootLDAP = New DirectoryEntry("LDAP://RootDSE")
            strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString

            oq = New System.Management.ObjectQuery("Select * from Win32_Session")
            query = New ManagementObjectSearcher(WMIScope, 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").ToString().Equals("2") Then
                    '  2 - for logged on User
                    consoleLogin = True
                   
                    Dim dmtfDate As String = oReturn("StartTime").ToString
                    Dim returnValue As DateTime
                    returnValue = ManagementDateTimeConverter.ToDateTime(dmtfDate)
                    consoleTime = (returnValue & ",Console Login")
                    Return consoleTime
                End If



                If oReturn("LogonType").ToString().Equals("10") Then
                    remoteLogin = True

                    Dim dmtfDate As String = oReturn("StartTime").ToString
                    Dim returnValue As DateTime
                    returnValue = ManagementDateTimeConverter.ToDateTime(dmtfDate)
                    remoteTime = (returnValue & ",Remote Desktop Login")

                End If

            Next

            Return remoteTime

        Catch ex As Exception
            Return ""
        End Try

    End Function
Avatar of DarkoLord
DarkoLord
Flag of Slovenia image

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?
Avatar of chadmanvb
chadmanvb

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
Avatar of DarkoLord
DarkoLord
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried that, but I think the syntax is wrong.  Where would I put that line to get relatedQuery?
Got it, works great.  Thanks!