I'm using a function to retrieve properties from a DirectoryServices object.
Unfortunately, the function fails whenever I trry to assign the object properties "accountExpires" to a string value with the error: System.InvalidCastException: Cast from type '_ComObject' to type 'String' is not valid.
The function I am using is as follows:
Public Function GetObjectProperties(ByVal DN As String, ByVal PropertyName As String) As String
Dim strOutput As String
Dim DE As New DirectoryEntry
DE.Path = "LDAP://" & ServerName & "/" & DN
DE.Username = AdminUsername
DE.Password = AdminPassword
If DE.SchemaClassName = "user" Then
strOutput = CStr(DE.Properties(PropertyName).Value)
Catch ex As Exception
'TODO: Raise an error
strOutput = "Exception! Input DN was = " & DN & "<BR>The error was: " & ex.ToString
I know this function works, as it WILL retreive other properties, such as 'CN'. But it dies on trying to retrieve the expiration date.