Monday, June 14, 2010

Using C# to find all CNs in ActiveDirectory

Usage:

To list all computers on the network where Microsoft Active Directory is used.

I faced this problem when I wished to document all computers in the local domain of this company. I found J.O.Donnell's C# solution posted on Auguest 27, 2002. I modified it to make it more flexiable to our case.


Discription:

In Active Directory there is an OU or Organizational Unit. It is actually a folder with a category of computers inside. I assumed that each computer has a LDAP path which is something like LDAP://domain OU=Workstations CN=localhost. In my design I will read all CNs in a specific domain with or without given OU.

The C# solution is straight forward. I always like a simple answer.


Execution:

C:> ListCN.exe domain contains notContains

E.g. Use the command bellow to find out all non-obsoleted Workstations and Servers in the domain of MFCHINA:

C:> ListCN.exe MFCHINA Workstations;Servers Obsoleted

//ListCN //Displays all computer names in an Active Directory //Written 08/26/02 - John O'Donnell - csharpconsulting@hotmail.com //Modified 06/14/10 - River Liu - output all workstations in certain LDAP
using System; using System.DirectoryServices; using Microsoft.Office.Core; using Microsoft.CSharp; using Excel = Microsoft.Office.Interop.Excel; using System.Collections.Generic; using System.Text; namespace ActiveDirectorySearch1 { class Class1 { static void Main (string[] args) { String domain=""; String contain=""; String notContain=""; if (args.Length <= 0) Environment.Exit(-1); else { domain = args[0]; } if (args.Length >= 2) { contain = args[1]; } if (args.Length >= 3) { notContain = args[2]; } String ldap = "LDAP://" + domain; String[] contains = contain.Split(';'); String[] notContains = notContain.Split(';'); DirectoryEntry entry = new DirectoryEntry(ldap); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(objectClass=computer)"); Console.WriteLine("Listing of computers in the Active Directory"); Console.WriteLine("============================================");
foreach(SearchResult resEnt in mySearcher.FindAll()) { String path = resEnt.GetDirectoryEntry().Path.ToString(); String cn; Boolean contained = true; Boolean containedNot = false; foreach (string con in contains) { if (String.Compare(con, "") == 0); else contained &= path.Contains(con); } foreach (string notCon in notContains) { if (String.Compare(notCon, "") == 0); else containedNot |= path.Contains(notCon); } if (contained == true && containedNot == false) { cn = resEnt.GetDirectoryEntry().Name.ToString(); cn = cn.Substring(3); Console.WriteLine(cn); using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"CNlist.inf", true)) { file.WriteLine(cn); //file.WriteLine(cn + "," + path); } } } Console.WriteLine("=========== End of Listing ============="); } } }
More ...

Biweekly Report - Report 1

week 1 – week 2 (1/6 – 11/6)

1. Things I did:

In the first week I leant about the organization, principles and people of this company. I worked on the installation and language features of MS Office 2003. I upgraded and installed MS Office 2003 Standard Edition for four colleagues, and successfully realized Traditional Chinese - Simplified Chinese conversion in English Office. Also I worked on Ubuntu9.10 and the recently released Ubuntu10.04 LTS. I installed both of them, and worked out the procedure of configuring printers in Ubuntu system. Besides, I tried to solve gnome desktop issue of Ubuntu10.04. To deal with issues shown up occasionally, I searched internet and other resources for information.

In the second week my primary job is to upgrade MS Office 2000 to MS Office 2003, and install .NET3.5 in more than 60 work stations. I did it by arranging a schedule for each group of four PCs, and coding to implement auto-installation for several PCs, in order to minimize manual work and installation duration. Also I helped colleagues to solve desktop issues that shown up after the upgrade.

2. Things I learnt

I was getting familiar with the structure and organization of this company. Secondly I observed how other people were dressing, behaving and working; and I was learning how to behave as an employee and work together with colleagues. Thirdly I had a better understanding of LAN and networking. Fourthly I knew better about the MS Office, Windows commands, Windows Managementation Instrumentation (wmi), Microsoft Installer, Linux commands and Ubuntu system. Fifthly I learnt to optimize the process of installation with minimal time and manual work. Lastly I learnt some tips to manage time efficiently, and make arrangement among a relatively large group of people. For technical problems, I searched Internet for references. For others, I asked colleagues for help.

3. Expectations

I wish to help to develop a mechanism to management software system in all computers, and to realize software’s uninstallation and installation via network. I want to do better to support daily desktop work.

4. Feelings

I found that all colleagues here are quite friendly. I enjoyed the work environment very much. I also enjoyed the work experience and I tried my best to optimize the process to become more efficiently.

More ...