troubleshooting Question

get login time and how the user logged in with .net 2.0 visual basic

Avatar of chadmanvb
chadmanvb asked on
.NET ProgrammingVisual Basic.NET
5 Comments1 Solution824 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
DarkoLord

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros