namespace EnabledUsers
{
class Program
{
public static string DomainName { get; }
static void Main(string[] args)
{
GetUsersInGroup();
}
public static void GetUsersInGroup()
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, DomainName);
UserPrincipal userPrin = new UserPrincipal(ctx) { Enabled = true };
userPrin.Name = "*";
var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher();
searcher.QueryFilter = userPrin;
var results = searcher.FindAll();
if (results != null)
{
foreach (Principal p in results)
{
Console.Write(p.SamAccountName);
Console.WriteLine(p.Name);
var creationDate = string.Empty;
var lastLogon = string.Empty;
var pwdlastset = string.Empty;
var company = string.Empty;
var prop = string.Empty;
var directoryEntry = p.GetUnderlyingObject() as DirectoryEntry;
prop = "whenCreated";
if (directoryEntry.Properties.Contains(prop))
{
creationDate = directoryEntry.Properties[prop].Value.ToString();
}
prop = "lastlogon";
if (directoryEntry.Properties.Contains(prop))
{
lastLogon = directoryEntry.GetLastLogon().ToString();
}
prop = "pwdLastSet";
if (directoryEntry.Properties.Contains(prop))
{
pwdlastset = directoryEntry.GetLastPwdChange().ToString();
}
prop = "company";
if (directoryEntry.Properties.Contains(prop))
{
company = directoryEntry.Properties[prop].Value.ToString();
}
Console.Write(creationDate);
Console.Write(lastLogon);
Console.Write(pwdlastset);
Console.Write(company);
try
{
Document myDocument = new Document(PageSize.A4.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));
myDocument.Open();
iTextSharp.text.Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 6);
iTextSharp.text.Font font6 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);
PdfPTable header = new PdfPTable(1);
PdfPTable table = new PdfPTable(6);
float[] columnWidths = new float[] { 15f, 15f, 15f, 15f, 15f, 15f };
table.SetWidths(columnWidths);
table.AddCell(new PdfPCell(new Phrase("Name", font6)));
table.AddCell(new PdfPCell(new Phrase("LanID", font6)));
table.AddCell(new PdfPCell(new Phrase("When Created", font6)));
table.AddCell(new PdfPCell(new Phrase("Last Logon", font6)));
table.AddCell(new PdfPCell(new Phrase("Last Password Reset", font6)));
table.AddCell(new PdfPCell(new Phrase("Company", font6)));
table.AddCell(new Phrase(p.Name, font5));
table.AddCell(new Phrase(p.SamAccountName, font5));
table.AddCell(new Phrase(creationDate, font5));
table.AddCell(new Phrase(lastLogon, font5));
table.AddCell(new Phrase(pwdlastset, font5));
table.AddCell(new Phrase(company, font5));
myDocument.Add(table);
myDocument.Close();
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
}
}
}
}
}