如何创建执行搜索功能并将其导出到 PDF 文档的任务计划程序?

How to create a Task Scheduler executing a search function and exporting it to a PDF document?

我需要创建一个任务计划程序,它能够 运行 一个搜索 Active Directory 的控制台应用程序,然后将该信息导出到 PDF 文件。目前在我的应用程序中,我可以手动完成它,它会显示在 ListView 中,然后我可以将该信息导出到 PDF 文档。

所以我创建了一个控制台应用程序,但它只显示我 PDF 中搜索的最后一行?

欢迎提出任何想法或建议。

编辑

这是我目前的代码:

 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);
            }

        }
    }
    }
}
}

我认为是因为这些行

Document myDocument = new Document(PageSize.A4.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

不应该在你的循环中。基本上每次处理新结果时都会创建一个新的 PDF,并同时覆盖以前的版本。

您需要在循环开始之前创建一次 PDF,然后不断向同一文档添加更多内容。将这些行移到 foreach (Principal p in results) 之前,它应该可以工作。