I needed to set the accountExpires property in AD DirectoryEntry could not find a simple answer. Found some information;
http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/182bfb6a-8b23-4c96-9379-101a4d91241a
http://www.rlmueller.net/AccountExpires.htm
I saw several articles re ADS ****. dll but didn't think i need to use this method
Dim valueToSet As Date = Now.AddDays(10) Dim ADSPath As String = "LDAP://cn=..." Dim de As DirectoryEntry = New DirectoryEntry(ADSPath) Dim d As TimeSpan = valueToSet.ToUniversalTime - Date.Parse("01/01/1601") Dim ValueToSetAsString As String = d.Ticks.ToString ' it appears that the ticks value is too large for the value of the directory entry ' converting to a string (18 chars or so) works! de.Properties("accountexpires").Value = ValueToSetAsString
Thanks to Brian, it seems like a lot of the code written above can be simplified;
de.Properties("accountexpires").Value = valueToSet.ToFileTime.ToString
Function returning AccountExpires and other bigInteger problems in VB.NET
Function ConvertADValueToDateTime(ByVal li As Object) As DateTime ' http://bytes.com/topic/visual-basic-net/answers/512901-lastlogontimestamp Try Dim lngHigh = li.HighPart Dim lngLow = li.LowPart Dim lastLogon = (lngHigh * 2 ^ 32) - lngLow Dim returnDateTime As DateTime = DateTime.FromFileTime(lastLogon) Return returnDateTime Catch ex As Exception Return Nothing End Try End Function
Usage example:
Dim d As DateTime = ConvertADValueToDateTime(de.Properties("accountexpires").value) If d = "01/01/1601" Then ' no expiry date Return Nothing Else Return d End If
Alternative method
Convert LDAP account to DateTime in C #
source share