HatchIT
asked on
lastLogon format from AD (via LDAP)
Can someone please tell me what I'm doing wrong here? I've been trying to get this (seemingly) simple thing to work, but it seems to be beyond me. I'm trying to write a VB.NET program to get the last logon time for a user. The form has a combo box that is loaded with sites at startup. When a site is selected, a list box gets all the users in that site. When the username is double-clicked, it calls the following code:
Sub CheckLastLogon(ByVal strSite As String, ByVal strUser As String)
Dim strUserDN As String = "LDAP://CN=" & strUser & ",OU=Users,OU=" & strSite & strDomain
Dim deUser As New DirectoryEntry(strUserDN)
Dim pvcLast As PropertyValueCollection = deUser.Properties("lastLog on")
'??????
End Sub
The problem is that I can't for the life of me figure out what to do with the "pvcLast" variable once I've retrieved it. For now, I'd like to just get it to display in a MsgBox so I know that I'm retrieving it correctly; I can do more with it later, once I've gotten it in the first place. Most things I try to do to it either give me a "Type Mismatch" error or, even more helpful (to me, at least) "System.ArgumentException - Argument 'Prompt' cannot be converted to type 'String'." or some such. The VBScript version of doing this seems so easy, why should the VB.NET version be so difficult? Grr.
PS: Yes, I realize that later I'm going to have to modify it so that it'll check all domain controllers. Right now I just want to see that I can get the lastLogon value, and successfully convert it into a valid date/time.
Sub CheckLastLogon(ByVal strSite As String, ByVal strUser As String)
Dim strUserDN As String = "LDAP://CN=" & strUser & ",OU=Users,OU=" & strSite & strDomain
Dim deUser As New DirectoryEntry(strUserDN)
Dim pvcLast As PropertyValueCollection = deUser.Properties("lastLog
'??????
End Sub
The problem is that I can't for the life of me figure out what to do with the "pvcLast" variable once I've retrieved it. For now, I'd like to just get it to display in a MsgBox so I know that I'm retrieving it correctly; I can do more with it later, once I've gotten it in the first place. Most things I try to do to it either give me a "Type Mismatch" error or, even more helpful (to me, at least) "System.ArgumentException - Argument 'Prompt' cannot be converted to type 'String'." or some such. The VBScript version of doing this seems so easy, why should the VB.NET version be so difficult? Grr.
PS: Yes, I realize that later I'm going to have to modify it so that it'll check all domain controllers. Right now I just want to see that I can get the lastLogon value, and successfully convert it into a valid date/time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Heh, two things that nothing I'd been able to find gave me the two peices of info I needed:
1) Add reference to "Active DS Type Library" COM object. Even that MS article you sent me said to use activeds.dll, but in the COM reference list, "Active DS Type Library" uses activeds.tlb
2) Use the IADsLargeInteger type to handle the data.
Below is what's working for me now:
1) Add reference to "Active DS Type Library" COM object. Even that MS article you sent me said to use activeds.dll, but in the COM reference list, "Active DS Type Library" uses activeds.tlb
2) Use the IADsLargeInteger type to handle the data.
Below is what's working for me now:
ASKER
Oops, meant to click in the comment box, missed and clicked "Submit" :-P Continued from last post...
Sub CheckLastLogon(ByVal strSite As String, ByVal strUser As String)
Dim strUserDN As String = "LDAP://CN=" & strUser & ",OU=Users,OU=" & strSite & strDomain
Dim deUser As New DirectoryEntry(strUserDN)
Dim intLast As IADsLargeInteger = DirectCast(deUser.Properti es("lastLo gon").Valu e, IADsLargeInteger)
Dim lngLast As Long = intLast.HighPart * 4294967296 + intLast.LowPart
MsgBox(DateTime.FromFileTi me(lngLast ))
deUser.Close()
deUser.Dispose()
End Sub
Thanks for that, jhenry. :-)
Sub CheckLastLogon(ByVal strSite As String, ByVal strUser As String)
Dim strUserDN As String = "LDAP://CN=" & strUser & ",OU=Users,OU=" & strSite & strDomain
Dim deUser As New DirectoryEntry(strUserDN)
Dim intLast As IADsLargeInteger = DirectCast(deUser.Properti
Dim lngLast As Long = intLast.HighPart * 4294967296 + intLast.LowPart
MsgBox(DateTime.FromFileTi
deUser.Close()
deUser.Dispose()
End Sub
Thanks for that, jhenry. :-)
Dim user As DirectoryEntry
Try
user = New DirectoryEntry
user.Path = theLdapPath
' use the next 3 lines when your current login account doesn't have enough permission
user.Username = "theDomainName\theUserName
user.Password = "thePassword"
user.AuthenticationType = AuthenticationTypes.None
user.RefreshCache()
Dim int64Val As IADsLargeInteger = DirectCast(user.Properties
Dim longInteger As Int64 = int64Val.HighPart * 4294967296 + int64Val.LowPart
Console.WriteLine(DateTime
Catch ex As COMException
Throw
Finally
If Not IsNothing(user) Then
user.Close()
user.Dispose()
End If
End Try
End Sub