Syntax question enumerating Active Directory Group Properties

A

AdamKadmon

I already tried posting this yesterday and it didn't post...

In a nutshell, I have a semi-functional app that does a few things
including searching the entire Active Directory for Groups and putting
the values of the CN, Description, and Info fields into a database.
The issue that I am experiencing is that when the foreach loop hits a
group with a null value in the description or info fields (the value
should be "") the app carry's the previous value over until it hits a
group that actually has a value. Then that value becomes "The Value"
that all the next value-less groups have displayed.

Perhaps some code will make this clearer:

<code>

using System;
using System.Data;
using System.DirectoryServices;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Management;
using System.Data.SqlClient;
using System.DirectoryServices.ActiveDirectory;
using System.Text.RegularExpressions;
using System.Text;

namespace ADGroupEnum
{
class ADGroupEnum
{

static void Main(string[] args)
{
string strName = "";
string strInfo = "";
string strComment = "";
DirectoryEntry strLDAP = new
DirectoryEntry(@"LDAP://SERVER:389/OU=ROOT,DC=COMPANY,DC=com");
DirectorySearcher mySearcher = new
DirectorySearcher(strLDAP);
mySearcher.Filter = ("(objectClass=group)");
foreach (SearchResult resEnt in mySearcher.FindAll())
{
string strGroupName =
resEnt.GetDirectoryEntry().Name.ToString();
Match dl = Regex.Match(strGroupName, "(DL)");
Match USBOU1 = Regex.Match(strGroupName, "(USBOU1)");
/* All of our distribution lists begin with DL */
if (dl.Success)
{ }
else
{
/* All of our groups begin with USBOU1 */
if (USBOU1.Success)
{
foreach (string key in
resEnt.GetDirectoryEntry().Properties.PropertyNames)
{
/* I only want "cn", "description", and
"info" fields. I have verified that these fields all are valid and
have a VBSCRIPT that can properly collect this information */

foreach (object o in
resEnt.GetDirectoryEntry().Properties[key])
{
if (key == "cn")
{
if (o.ToString() == "")
{
strName = "";
}
else
{
strName = o.ToString();
}
}
else if (key == "description")
{
if (o.ToString() == "")
{
strComment = "";
}
else
{
strComment = o.ToString();
}
}
else if (key == "info")
{
if (o.ToString() == "")
{
strInfo = "";
}
else
{
strInfo = o.ToString();
}
}
}
}Writem(strName, strComment, strInfo);
}
}
}
}
public static void Writem(string name, string comment, string
info)
{
Console.WriteLine(name + "\n" + comment + "\n" + info +
"\n" + "============================================\n");

}

}
}

</code>

What am I doing wrong here? I thought that the if (o.ToString() == "")
bit would get it to do what I wanted, but it doesn't seem to make any
difference.

Can anyone help?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top