public bool AddComputerToGroup(string computerName, string groupName) { bool done = false; GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName); if (group == null) { group = new GroupPrincipal(context, groupName); } ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(context, computerName); if (computer != null & group != null) { group.Members.Add(computer); group.Save(); done = (computer.IsMemberOf(group)); } return(done); }
private void button1_Click(object sender, EventArgs e) { txtActionLog.AppendText("Checing connection to " + txtSearch.Text.ToString() + "..." + Environment.NewLine); log("Connection attempt has been made to " + txtSearch.Text.ToString()); string ComputerName = txtSearch.Text.ToString(); if (rbtnIP.Checked) { ComputerName = GetMachineNameFromIPAddress(txtSearch.Text.ToString()); ComputerName = ComputerName.Replace(".corp.skf.net", ""); } string DomainName = Environment.UserDomainName; bComputerFound = false; try { using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, DomainName)) { ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, ComputerName); if (computer != null) { bComputerFound = true; GroupPrincipal group = GroupPrincipal.FindByIdentity(domainContext, sPCGroup); if (computer.IsMemberOf(group)) { bComputerAllowed = true; } else { bComputerAllowed = false; } } else { bComputerFound = false; } } } catch (Exception) { bComputerFound = false; } if (bComputerFound) { if (bComputerAllowed) { timer1.Tick += new EventHandler(timer_Tick); timer1.Enabled = true; timer1.Start(); ThreadStart childref3 = new ThreadStart(LaunchCommandLineApp); Thread LaunchRemoteTool = new Thread(childref3); LaunchRemoteTool.Start(); } else { MessageBox.Show(ComputerName + ": You are not allowed to connect to this Computer", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); txtActionLog.AppendText(ComputerName + ": not allowed to connect!" + Environment.NewLine); log(ComputerName + ": not allowed to connect!"); } } else { if (bIPFound) { MessageBox.Show(ComputerName + " does not exist! Please check your spelling.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); txtActionLog.AppendText(ComputerName + " does not exist!" + Environment.NewLine); log(ComputerName + " does not exist!"); } else { MessageBox.Show(txtSearch.Text.ToString() + " is incorrect! Please check again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); txtActionLog.AppendText(txtSearch.Text.ToString() + " is incorrect!" + Environment.NewLine); log(txtSearch.Text.ToString() + " is incorrect!"); } } }