private void BtnDelete_Click(object sender, EventArgs e) { DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Are you sure you want to permanently delete this computer from active directory?", "Warning!", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { DialogResult dialogResult1 = System.Windows.Forms.MessageBox.Show("Are you sure? This action cannot be undone!", "Warning!", MessageBoxButtons.YesNo); if (dialogResult1 == DialogResult.Yes) { try { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(ctx, tbSearch.Text); if (computer != null) { computer.Delete(); } else { return; } } catch (Exception) { } } } }
/// <summary> /// This method receives a string containing the server name, and deletes it's computer account. /// </summary> /// <param name="serverName">a string containing the server name.</param> /// <returns>A ComputerAccountResult containing the action performed, a success/failure message, /// the name of the server which the computer account belongs to and the path of the object in AD.</returns> public static ComputerAccountResult DeleteComputerAccount(string serverName) { // Set up the result object. ComputerAccountResult result = new ComputerAccountResult() { action = "delete", message = string.Empty, serverName = serverName, objectADPath = string.Empty }; // Set up domain context. PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain); // Check if an existing computer account exists in AD. ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(pc, serverName); if (computer != null) { result.objectADPath = computer.DistinguishedName; computer.Delete(); result.message = "Computer Account deleted successfully."; } else { result.message = "No such Computer Account."; } return(result); }
/// <summary> /// Deletes the specified computer account. /// </summary> /// <param name="name">The unique identifier of the computer.</param> /// <returns>True if the computer was deleted, false if not or the computer doesn't exist.</returns> public static Boolean DeleteComputer(string name) { try { ComputerPrincipal c = ComputerPrincipal.FindByIdentity(GetPrincipalContext(), name); c.Delete(); c.Save(); return(true); } catch { return(false); } }
public static void DeleteComputerAccountVoid(string serverName) { // Set up domain context. PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain); // Check if an existing computer account exists in AD. ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(pc, serverName); while (computer == null) { computer = ComputerPrincipal.FindByIdentity(pc, serverName); } if (computer != null) { computer.Delete(); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); String htmlOptions = li.HtmlOptions; String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); List <RunPayloadItem> thisPL = RunPayload.RunPayloadItems.Where(p => p.ItemID == li.ItemID).ToList(); string compAcct = thisPL.Where(p => p.ElementID == "CompAcct").FirstOrDefault().ElementValue; PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); ComputerPrincipal comp = ComputerPrincipal.FindByIdentity (context, compAcct); if (comp == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", compAcct), TimeDone = DateTime.Now }); } comp.Delete(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("Computer name: {0} was removed from Active Directory.", compAcct), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }