471,618 Members | 1,315 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Syntax question enumerating Active Directory Group Properties

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.PropertyName s)
{
/* 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!

Jan 10 '06 #1
1 2820
Oh, I guess it did post, I just couldn't find it under the main CSharp
group.

Jan 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Technical Group | last post: by
1 post views Thread by tangus via DotNetMonster.com | last post: by
2 posts views Thread by Jim in Arizona | last post: by
3 posts views Thread by =?Utf-8?B?YXppZWdsZXI=?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.