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.Director yServices;
using System.Configur ation;
using System.Collecti ons;
using System.IO;
using System.Manageme nt;
using System.Data.Sql Client;
using System.Director yServices.Activ eDirectory;
using System.Text.Reg ularExpressions ;
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=COMP ANY,DC=com");
DirectorySearch er mySearcher = new
DirectorySearch er(strLDAP);
mySearcher.Filt er = ("(objectClass= group)");
foreach (SearchResult resEnt in mySearcher.Find All())
{
string strGroupName =
resEnt.GetDirec toryEntry().Nam e.ToString();
Match dl = Regex.Match(str GroupName, "(DL)");
Match USBOU1 = Regex.Match(str GroupName, "(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.GetDirec toryEntry().Pro perties.Propert yNames)
{
/* I only want "cn", "descriptio n", 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.GetDirec toryEntry().Pro perties[key])
{
if (key == "cn")
{
if (o.ToString() == "")
{
strName = "";
}
else
{
strName = o.ToString();
}
}
else if (key == "descriptio n")
{
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.WriteLi ne(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!
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.Director yServices;
using System.Configur ation;
using System.Collecti ons;
using System.IO;
using System.Manageme nt;
using System.Data.Sql Client;
using System.Director yServices.Activ eDirectory;
using System.Text.Reg ularExpressions ;
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=COMP ANY,DC=com");
DirectorySearch er mySearcher = new
DirectorySearch er(strLDAP);
mySearcher.Filt er = ("(objectClass= group)");
foreach (SearchResult resEnt in mySearcher.Find All())
{
string strGroupName =
resEnt.GetDirec toryEntry().Nam e.ToString();
Match dl = Regex.Match(str GroupName, "(DL)");
Match USBOU1 = Regex.Match(str GroupName, "(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.GetDirec toryEntry().Pro perties.Propert yNames)
{
/* I only want "cn", "descriptio n", 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.GetDirec toryEntry().Pro perties[key])
{
if (key == "cn")
{
if (o.ToString() == "")
{
strName = "";
}
else
{
strName = o.ToString();
}
}
else if (key == "descriptio n")
{
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.WriteLi ne(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!
Comment