Michael
Michael

Reputation: 1

I need assistance with verifying computers in AD and AD group C#

This code currently checks the boxes in Columns "Found_IN_AD" for every single retrieved item whether they exist in AD or not. The column "Computer_IN_AD_Group" is checked correctly for one CPU_Code that is selected and then when you select another CPU_Code the result is completely off. One of the CPU_Code I select has ever both boxes for every row checked even though there are values that do not even exist in AD.

My goal is to fix the code and if a value does not exist in AD leave both columns empty don't check the box and if an item exists in AD but not the group only check the box for the AD column, if an item exists in GROUP, then that means it exists in AD so check both boxes. Not sure what I am doing wrong.

I tried the code that is below.

private bool MachineADCheck(string ComputerName)
{
    

    try
    {
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
        {
            ComputerPrincipal cp = ComputerPrincipal.FindByIdentity(ctx, ComputerName);

            if (cp != null)
            {
                return true;
            }
            return false;
        }

    }
    catch
    {
        return false;
    }
}
        
private bool CheckMachine_ADGroup (string ComputerName, string GroupName)
{
   
    try
    {
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
        {
            ComputerPrincipal cp = ComputerPrincipal.FindByIdentity(ctx, ComputerName);
            if (cp != null)
            {
                GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, GroupName);
                return gp != null && cp.IsMemberOf(gp);
            }
            return true;
        }
    }
    catch
    {
        return false;
    }
}

    
private void combo_box_SelectionChanged(object sender, RoutedEventArgs e)
{
    ComboBox cbx = (ComboBox)sender;
    if (cbx != null && cbx.SelectedValue != null)
    {
        if (cbx.Name == "combo_box")
        {
            Room_code = cbx.SelectedValue.ToString();
            DataTable ComputerData = ComputerWorkStation(Room_code);
            ComputerData.Columns.Add("Member in AD", typeof(bool));
            ComputerData.Columns.Add("AD Group", typeof(bool));


            foreach (DataRow row in ComputerData.Rows)
            {
                string ComputerName= row["Computer_Name"].ToString();
                bool ADCheck= false;
                bool InACGroup = false;


                ADCheck= MachineADCheck(ComputerName);
                if(!ADCheck)
                {

                    InACGroup = CheckMachine_ADGroup(ComputerName, "Authorized Computers");
                }
                else
                {
                    if (!ComputerName.StartsWith(Room_Code))
                    {
                        ComputerName= Room_Code+ ComputerName;
                        ADCheck = MachineADCheck(ComputerName);
                        if (ADCheck)
                        {  
                            InACGroup = CheckMachine_ADGroup(ComputerName, "Authorized Computers");
                        }
                        else
                        {
                            string ComputerID= row["Computer_ID"].ToString();
                            ComputerName = ComputerName + ComputerID;
                            ADCheck = MachineADCheck(ComputerName);
                            if (ADCheck)
                            {
                               
                                InACGroup= CheckMachine_ADGroup(ComputerName, "Authorized Computers");
                            }
                        }
                    }
                }

                row["Member in AD"] = ADCheck;
                row["AD Group"] = InACGroup;
            }

            results2.ItemsSource = ComputerData.DefaultView;
            results2.Visibility = Visibility.Visible;

        }
    }
}
    
public DataTable ComputerWorkStation(string RoomCodes)
{
    
    DataTable table = new DataTable();
    using (SqlConnection con = new SqlConnection(Authorized_DB))
    {
        string sql = string.Concat("SELECT Computer_Name, Computer_ID FROM Computer_Workstation WITH (NOLOCK) WHERE Room_Codes = '", RoomCodes, "'");
        SqlDataAdapter ComputerWorkStationAdapter = new SqlDataAdapter(sql, con);

        try
        {
            con.Open();
            ComputerWorkStationAdapter.Fill(table);

        }
        
        catch (Exception ex)
        {
            MessageBox.Show("Error Check Your Code" + ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    return table;

}
    }
}

Upvotes: 0

Views: 23

Answers (0)

Related Questions