Solved

Store Active Directory Querry into a Multidimensional array

Posted on 2007-11-18
4
381 Views
Last Modified: 2013-12-17
Essentially I want to query 2 domain controllers in AD and compare the information between the two.  I only need to be able to store the HostName and Last Login time stamp from each Domain Controller.  I would like to modify the code to return the array to the calling function.  

I----------------------------------------------------------------------------------------------------------------------
       private void Button_1_Click(object sender, EventArgs e)
        {
                string OUDN = txtTargetList_Path.Text;
                try
                {
                    DirectoryEntry entry = new DirectoryEntry("LDAP://" + OUDN);
                    DirectorySearcher DirSearch = new DirectorySearcher(entry);
                    DirSearch.Filter = "(objectClass=computer)";
                    DirSearch.PageSize = 100000;

                    foreach(SearchResult results in DirSearch.FindAll())
                    {
                        string Line = "";
                        string HName = "";
                        string LstLgn = "";
                       
                        foreach(string Value in results.Properties.PropertyNames)
                        {
                            if(results.Properties[Value]!=null)
                            {
                                if (Value == "name")
                                {
                                    HName = (string)results.Properties[Value][0];
                                }
                                if(Value == "lastlogon")
                                {
                                    long lastLogon = (long)results.Properties[Value][0];
                                    LstLgn = DateTime.FromFileTime(lastLogon).ToString();
                                }
                            }
                        }
                        Line = HName + "," + LstLgn;
                        listBox1.Items.Add(Line);
                    }
                }
                catch (IOException Iox)
                {
                    lstErrorLog.Items.Add(Iox.Data.ToString());
                }
                catch (DirectoryServicesCOMException ex)
                {
                    lstErrorLog.Items.Add(ex.Message.ToString());
                }
        }
0
Comment
Question by:Trypsim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20308407
The code below shows how to build and return an ArrayList from a function.
using System;
using System.Collections;
 
namespace clrtest
{
    // Data will represent one element in the array
    class Data
    {
        public Data(string name_, string lastlogin_)
        {
            m_name = name_;
            m_lastlogin = lastlogin_;
        }
 
        // Accessor for name
        public string Name
        {
            get
            {
                return m_name;
            }
        }
 
        // Accessor for lastlogin
        public string LastLogin
        {
            get
            {
                return m_lastlogin;
            }
        }
 
        private string m_name;
        private string m_lastlogin;
    }
 
    class Program
    {
        // Dummy function just to demonstrate how to return array
        static ArrayList foo()
        {
            // Build a dummy array
            ArrayList al = new ArrayList();
            al.Add(new Data("John Doe", "18/11/2007"));
            al.Add(new Data("Jane Smith", "18/11/2007"));
 
            // Return array
            return al;
        }
 
        static void Main(string[] args)
        {
            // foo() will return an ArrayList object
            ArrayList al = foo();
 
            // Iterate through items in ArrayList
            foreach (Object obj in al)
            {
                // Cast boxed object to Data element
                Data data = obj as Data;
 
                // IF cast was successful output the data as proof
                if (data != null)
                {
                    Console.WriteLine(data.Name);
                    Console.WriteLine(data.LastLogin);
                }
            }
        }
    }
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 20308444
So you'd need to change your function to something like this...
private ArrayList Button_1_Click(object sender, EventArgs e)
{
        //////////////////////////////////////////////////////////////////////
        // Create new array
        ArrayList al = new ArrayList();
        //////////////////////////////////////////////////////////////////////
 
        string OUDN = txtTargetList_Path.Text;
        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + OUDN);
            DirectorySearcher DirSearch = new DirectorySearcher(entry);
            DirSearch.Filter = "(objectClass=computer)";
            DirSearch.PageSize = 100000;
 
            foreach(SearchResult results in DirSearch.FindAll())
            {
                string Line = "";
                string HName = "";
                string LstLgn = "";
                
                foreach(string Value in results.Properties.PropertyNames)
                {
                    if(results.Properties[Value]!=null)
                    {
                        if (Value == "name")
                        {
                            HName = (string)results.Properties[Value][0];
                        }
                        if(Value == "lastlogon")
                        {
                            long lastLogon = (long)results.Properties[Value][0];
                            LstLgn = DateTime.FromFileTime(lastLogon).ToString(); 
                        }
                    }
                }
                Line = HName + "," + LstLgn;
                listBox1.Items.Add(Line);
 
                //////////////////////////////////////////////////////////////
                // Add name/logintime to array
                al.Add(new Data(HName, LstLgn));
                //////////////////////////////////////////////////////////////
            }
        }
        catch (IOException Iox)
        {
            lstErrorLog.Items.Add(Iox.Data.ToString());
        }
        catch (DirectoryServicesCOMException ex)
        {
            lstErrorLog.Items.Add(ex.Message.ToString());
        }
 
        //////////////////////////////////////////////////////////////////////
        // Return array
        return al;
        //////////////////////////////////////////////////////////////////////
}

Open in new window

0
 

Author Closing Comment

by:Trypsim
ID: 31409808
Thanks, this is exactly what I was looking for.
0
 

Author Comment

by:Trypsim
ID: 20309478
al.Add(new Data(HName, LstLgn));

Returns 3 errors when I compile it?  I think that I am missing a Library.  Can you please help with this too?

Error      1      The type or namespace name 'data' could not be found (are you missing a using directive or an assembly reference?)      C:\Documents and Settings\tcbea\Desktop\VS Projects Nathan\SITS-SQT\SITS-SQT\SQT.cs      4433      32      SITS-SQT

Thanks,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question