wright1968
Member
I am trying to get some information from Active Directory so I can export the information into some reports.
I can use DSQUERY or ADFIND to get this information but I don't want my application to be dependent on those utilities being installed on the machine so I would like to get at the information myself using my own code.
However, every time I query Active Directory for the "lastLogonTimestamp" or "pwdLastSet" values, I get the System.InvalidCastException error Conversion from type '_ComObject' to type 'String' is not valid. I've tried several different methods, but nothing I do lets me get at the information stored in those fields.
Everything in my code below works just fine until the last two lines of the FOR loop:
I can use DSQUERY or ADFIND to get this information but I don't want my application to be dependent on those utilities being installed on the machine so I would like to get at the information myself using my own code.
However, every time I query Active Directory for the "lastLogonTimestamp" or "pwdLastSet" values, I get the System.InvalidCastException error Conversion from type '_ComObject' to type 'String' is not valid. I've tried several different methods, but nothing I do lets me get at the information stored in those fields.
Everything in my code below works just fine until the last two lines of the FOR loop:
VB.NET:
Try
Dim myADEntry As DirectoryEntry = New DirectoryEntry(strNTDomain)
Dim myADSearcher As DirectorySearcher = New DirectorySearcher(myADEntry)
myADSearcher.PageSize = 500
Dim myADResults As SearchResult
myADSearcher.Filter = ("(CN=" & sHost & ")")
For Each myADResults In myADSearcher.FindAll
sRawResult = myADResults.GetDirectoryEntry().Name.ToString()
strOUPath = myADResults.Path
sDescription = myADResults.GetDirectoryEntry.Properties("description").Value
sOS = myADResults.GetDirectoryEntry.Properties("OperatingSystem").Value
sOU = CleanOutput(myADResults.GetDirectoryEntry.Path.ToString)
sOSVersion = myADResults.GetDirectoryEntry.Properties("operatingSystemVersion").Value
sServicePack = myADResults.GetDirectoryEntry.Properties("operatingSystemServicePack").Value
sCreated = myADResults.GetDirectoryEntry.Properties("whenCreated").Value
sChanged = myADResults.GetDirectoryEntry.Properties("whenChanged").Value
sLocation = myADResults.GetDirectoryEntry.Properties("location").Value
sMachineRole = myADResults.GetDirectoryEntry.Properties("machineRole").Value
sNetworkAddress = myADResults.GetDirectoryEntry.Properties("networkAddress").Value
intUserAccountControl = myADResults.GetDirectoryEntry.Properties("userAccountControl").Value
Call CheckUserAccountControl() 'Determine if account is enabled or disabled.
sLastLogonTimestamp = myADResults.GetDirectoryEntry.Properties("lastlogontimestamp").Value
sPwdLastSet = myADResults.GetDirectoryEntry.Properties("pwdLastSet").Value
Next
myADSearcher.Dispose()
myADEntry.Dispose()
Catch ex As Exception
MsgBox(ex.Message, vbOKOnly, "Error")
Call ClearHostInfo(Nothing, Nothing)
Call ClearValues()
picNotFound.Visible = True
End Try