using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
namespace Pass_Expire
{
/// <summary>
/// Summary description for AD_Test.
/// </summary>
public class AD_Test
{
static DirectorySearcher mySearcher;
static ActiveDs.IADsUser iuser;
static DateTime lastChangedDate;
const Int64 iSeconds = 864000000000;
public AD_Test()
{
//Default constructor
}
[STAThread]
static void Main(string[] args)
{
getDomainUsers();
}
static void getDomainUsers()
{
using(DirectoryEntry entry = new DirectoryEntry("LDAP://<domainName>"))
{
//Determine the number of Max Password Aging scheme (days) of the domain
Int64 i = Math.Abs(GetInt64FromLargeInteger(entry.Properties["maxPwdAge"].Value)/iSeconds);
Console.WriteLine("Max Password Age: " + i.ToString() + " days");
//Set the directory path to point only to the authorized software installers OU
entry.Path="LDAP://<domainName>/OU=Authorized Software Installers,OU=Users OU,DC=<domainName>,DC=yourCompany,DC=com";
//Use the directory searcher to look for only class of users
mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=user)");
//Console.WriteLine("========================================");
}
//Loop through each user in the domain and write out properties
//to the console
foreach(SearchResult sr in mySearcher.FindAll())
{
//Enumerate through various user properties
try
{
using(DirectoryEntry de=sr.GetDirectoryEntry())
{
//Write out the user properties to the screen
Console.WriteLine("Display Name : " + de.Properties["DisplayName"].Value.ToString());
Console.WriteLine("User Name : " + de.Properties["sAMAccountName"].Value.ToString());
/* Convert the directory entry user object into the IADs user object
* this allows easier access to user properties */
iuser=(ActiveDs.IADsUser)de.NativeObject;
Int64 z = GetInt64FromLargeInteger(de.Properties["pwdLastSet"].Value);
//If PwdLastSet <> 0 then we have an active user
if(z != 0)
{
lastChangedDate = iuser.PasswordLastChanged;
Console.WriteLine("Date Password Changed: " +
lastChangedDate.ToShortDateString());
}
}
Console.WriteLine();
}
catch(COMException ex)
{
//Alert the user that an error occurred
Console.WriteLine("COM ERROR: " + ex.Message);
}
catch(Exception ex)
{
//Alert the user that an error occurred
Console.WriteLine("GENERIC ERROR: " + ex.Message);
}
finally
{
iuser = null;
}
}
}
private static Int64 GetInt64FromLargeInteger(object largeInteger)
{
//Declarations
Int32 low;
Int32 high;
byte[] valBytes = new byte[70];
try
{
ActiveDs.IADsLargeInteger longInt = (ActiveDs.IADsLargeInteger)largeInteger;
low = longInt.LowPart;
high = longInt.HighPart;
BitConverter.GetBytes(low).CopyTo(valBytes, 0);
BitConverter.GetBytes(high).CopyTo(valBytes, 4);
}
catch(COMException ex)
{
Console.WriteLine(ex.Message + "\n" + ex.Source);
return 0;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message + "\n" + ex.Source);
return 0;
}
finally
{
}
return BitConverter.ToInt64(valBytes, 0);
}
}//end of class
}//end of namespace