Using DirectorySearcher.FindAll() in WinPE results in COMException 0x80005000

0

I have a C# app that I've written that essentially scrapes the service tag number to generate a name to write to the Unattend.xml that gets injected when deploying Windows. One of the features our techs have requested is the ability to delete computers from Active Directory that contain that service tag number automatically. However, I'm getting an exception that appears to be about as generic as you can possibly get.

There are quite a few questions that target that error message and the DirectorySearcher class, but most of them are in reference to ASP.Net and/or WCF apps, so the answers are either A) already present in my code; or B) unrelated to my environment.

The method in the code below works inside of Windows without issue, so I have the feeling that this might be a WinPE dependency issue, and might need to be moved over to SuperUser or ServerFault (whichever).

Code is as follows:

public static void RemovePCsFromAD(string searchFor)
{
    List<string> pcList = new List<string>();
    var username = "redacted";
    var password = "redacted";
    var domain = new DirectoryEntry("LDAP://somecompany.com/DC=somecompany,DC=com",username,password);
    var search = new DirectorySearcher(domain);
    search.PropertiesToLoad.Add("name");
    search.Filter = "(&(objectClass=Computer)(name=*" + searchFor + "*))";
    search.Sort = new SortOption("name", SortDirection.Ascending);

    using (SearchResultCollection allThePCs = search.FindAll())
    {
        if (allThePCs != null)
        {
            foreach (SearchResult result in allThePCs)
            {
                pcList.Add(result.Properties["name"][0].ToString());
            }

            DialogResult ShouldIDelete = MessageBox.Show("The following PCs with service tag \"" + searchFor + "\" "were found in AD:" + Environment.NewLine + Environment.NewLine +
                pcList.ToString() + Environment.NewLine + Environment.NewLine +
                "Do you want to delete them from active directory?", "Your attention is required", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (ShouldIDelete == DialogResult.Yes)
            {
                PrincipalContext pContext = new PrincipalContext(ContextType.Domain);
                foreach (var pc in pcList)
                {
                    ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(pContext, pc);
                    if (computer != null)
                    {
                        computer.Delete();
                    }
                }
            }
        }
    }
}

This works perfectly well inside of Windows 10 Enterprise, however inside of WinPE I get the following exception:

enter image description here

I have also tried changing

var username = "redacted";

to

var username = @"somecompany.com\redacted";

without effect.

As I said before, this is likely an issue with WinPE more than my code, and if necessary, I can remove this and repost at the appropriate site, but if anybody has more experience writing apps for WinPE, any guidance would be greatly appreciated.

c#
comexception
winpe
directorysearcher
asked on Stack Overflow Jul 1, 2019 by jparnell8839

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0