Re: ADSI script implement in C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi, Steven,

Thanks for your research!

My customer does not like to add the reference since it's .NET application. Is it possible to achieve the same goal without using "WINNT" provider and use pure LDAP query?

Thanks a million!
John

"Steven Cheng [MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message news:WMnjN6gwIHA.4564@xxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for your reply John.

I'm not quite good at ADSI, however, based on my research, the problem here
seems due to the C# object doesn't be compatible with the underlying COM
object(used in ADSI interface underlyingly).

I've found a former thread mentioned this and some members have suggested
that we should add reference to the "activeds.tlb" COM component to do the
WINNT provider specific ADSI query:

#Getting local admin groups and users on a windows server using ADSI
http://www.megasolutions.net/cSharp/Getting-local-admin-groups-and-users-on-
a-windows-server-using-ADSI-27079.aspx


I have also tested it in my local environment and got it working. Here is
my test code:



...........

using ActiveDs;

static void RunADSI()
{
string domain = "domain name";
string machineName = "server name";
string groupName = "administrators";
IADsMembers MembersCollection = null;

string path = string.Format("WinNT://{0}/{1}/{2},group",
domain, machineName, groupName);

using (DirectoryEntry groupEntry =
new DirectoryEntry(path))
{
// invoke native method "members"
MembersCollection = groupEntry.Invoke("Members") as
IADsMembers;
object[] filter = { "User", "Group" };
MembersCollection.Filter = filter;
foreach (object obj in MembersCollection)
{
IADsGroup group = obj as IADsGroup;
if (group != null)
Console.WriteLine("group Name: {0}", group.Name);
//this is a group, so recurse to get the members of
this group....
else
{
IADsUser user = obj as IADsUser;
Console.WriteLine("User Name: {0}", user.Name);
}
}
}


}


#make sure you've import the reference to the "activeds.tlb" in system32
dir(you can use Add Reference to add it from "COM" tab in visual studio):


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Reply-To: "John" <pursca@xxxxxxxxxxx>
From: "John" <pursca2008@xxxxxxxxxxxxxxxx>
References: <A23A6FD7-9734-45F4-827C-4950635AC940@xxxxxxxxxxxxx>
<khCTpkTwIHA.5796@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: ADSI script implement in C#
Date: Wed, 28 May 2008 22:02:31 -0700


Hi, Steven,

in VB6 or VBscript,
Dim Group As IADsGroup
Dim objUser As Object

Set Group = GetObject("WinNT://DomainName/ComputerName/Administrators,
group")
For Each objUser In Group.Members
MsgBox objUser.Name & " - " & objUser.ADsPath & " - " &
objUser.Class
Next

returns correct information but

in C#,
string ADsPath =
"WinNT://DomainName/ComputerName/Administrators, group";
DirectoryEntry de = new DirectoryEntry(ADsPath);

foreach (DirectoryEntry d in de.Children)
{
Console.WriteLine(d.Name + " " + d.Path);
}

this does not return anything

Could you please help understand why?

Thanks!
John

"Steven Cheng [MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:khCTpkTwIHA.5796@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi John,

From your description and the VB code snippet, you're going to use C# to
perform ADSI query on some domain computer's properties. As far as I
know,
in .net, ADSI programming is provided by the System.DirectoryServices
namespace. And for accessing ADSI object, you can use the
"DirectoryEntry"
class. Here is the C# code snippt that help query the certain domain
computer and loop its properties:

===============
static void RunADSI()
{
DirectoryEntry en = new DirectoryEntry();
en.Path = "WinNT://mydomain/mycomputer";

foreach (string key in en.Properties.PropertyNames)
{
Console.WriteLine("{0}: {1}",key,
en.Properties[key].Value);
}
}
==============

the following ,knowledge base article provide the same information:

#How to access ADSI objects in Visual C#
http://support.microsoft.com/kb/315716

If you want more information and code about ADSI query in .net/c#, I
suggest you have a look at the following ADSI query tool, you can get
more
ideas from its code:

#BeaverTail (written entirely in C#)
http://adsi.mvps.org/adsi/CSharp/beavertail.html

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent
issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "John" <pursca@xxxxxxxxxxx>
From: "John" <pursca2008@xxxxxxxxxxxxxxxx>
Subject: ADSI script implement in C#
Date: Wed, 28 May 2008 17:15:00 -0700


Hi, gurus,
How can I implement the following feature in C#:
Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ",
group")
For Each objMember In objGroup.Members
WScript.Echo vbCrLf & " Name: " & objMember.Name
Next
Thanks!
John




.



Relevant Pages

  • Re: ADSI script implement in C#
    ... I'm not quite sure about a pure LDAP syntax query since AD is not my ... Microsoft MSDN Online Support Lead ... I'm not quite good at ADSI, however, based on my research, the problem ... DirectoryEntry de = new DirectoryEntry; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ADSI script implement in C#
    ... DirectoryEntry de = new DirectoryEntry; ... perform ADSI query on some domain computer's properties. ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ADSI script implement in C#
    ... I'm not quite good at ADSI, however, based on my research, the problem here ... MembersCollection = groupEntry.Invokeas ... Microsoft MSDN Online Support Lead ... DirectoryEntry de = new DirectoryEntry; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ADSI script implement in C#
    ... perform ADSI query on some domain computer's properties. ... If you want more information and code about ADSI query in .net/c#, ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: ADSI script implement in C#
    ... perform ADSI query on some domain computer's properties. ... If you want more information and code about ADSI query in .net/c#, ... Microsoft MSDN Online Support Lead ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.csharp)