By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,208 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,208 IT Pros & Developers. It's quick & easy.

ADSI script implement in C#

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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
Jun 27 '08 #2

P: n/a
Thanks Steven!

Could you please show me how to specify a Path that can get local group in
remote computer?

Thanks!
John

"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl...
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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
Jun 27 '08 #3

P: n/a
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]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl...
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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
Jun 27 '08 #4

P: n/a
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/...-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:
ms****@microsoft.com.

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

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
>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]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl...
>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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
================================================= =
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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
Jun 27 '08 #5

P: n/a
Hi John,

How are you doing?

Does my last reply help you further on this issue or have you tried the
code I provided? If there is anything else we can help, welcome to post
here.

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:
ms****@microsoft.com.

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

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: st*****@online.microsoft.com (Steven Cheng [MSFT])
Organization: Microsoft
Date: Fri, 30 May 2008 04:35:04 GMT
Subject: Re: ADSI script implement in C#
>Path: TK2MSFTNGHUB02.phx.gbl
Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.languages.csharp:90671
NNTP-Posting-Host: TOMCATIMPORT3 10.201.220.210

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/...s-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):
>>>>
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/subscripti...t/default.aspx.
================================================ ==
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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


Jun 27 '08 #6

P: n/a
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]" <st*****@online.microsoft.comwrote in message
news:WM**************@TK2MSFTNGHUB02.phx.gbl...
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/...-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:
ms****@microsoft.com.

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

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
>>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]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl.. .
>>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:
ms****@microsoft.com.

================================================ ==
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
================================================ ==
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
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

Jun 27 '08 #7

P: n/a
Thanks for your reply John,

I'm not quite sure about a pure LDAP syntax query since AD is not my
speciality. But I'll try to perform some research and discuss with some
other AD engineers to see whether they can provide any further information
on this.

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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.--------------------
>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
<72**********************************@microsoft.co m>
<WM**************@TK2MSFTNGHUB02.phx.gbl>
>Subject: Re: ADSI script implement in C#
Date: Thu, 5 Jun 2008 05:11:09 -0700
>
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]" <st*****@online.microsoft.comwrote in message
news:WM**************@TK2MSFTNGHUB02.phx.gbl...
>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/...-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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
>ications.

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
>>>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]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl. ..
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:
ms****@microsoft.com.

=============================================== ===
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
=============================================== ===
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>Reply-To: "John" <pu****@hotmail.com>
>From: "John" <pu********@community.nospam>
>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
>

Jun 27 '08 #8

P: n/a
Hi John,

I've just got some further information from some AD dev engineers. Here is
their comments on this issue:

Actually, LDAP provider is not suitable here. Because standalone or member
machines don't have an LDAP interface. Local groups are stored in the
machine's local SAM store, which doesn't have an LDAP server.

If the concerns is not to reference "activeds.tlb", typically, to avoid the
need for activeds.tlb,one way is just define the COM interopt interface
definitions by hand using the [ComImport] attribute, like the following:

==================================
internal class UnsafeNativeMethods
{
[ComImport, Guid("451a0030-72ec-11cf-b03b-00aa006e0975"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sDual)]
public interface IADsMembers {

int Count {
[return: MarshalAs(UnmanagedType.U4)]
get;
}

object _NewEnum {
[return: MarshalAs(UnmanagedType.Interface)]
get;
}

object Filter {
[return: MarshalAs(UnmanagedType.Struct)]
get;
[param: MarshalAs(UnmanagedType.Struct)]
set;
}
}
=================================

and in the original winnt provider code, use the above interface to cast
the object.

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:
ms****@microsoft.com.

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

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

--------------------
>From: st*****@online.microsoft.com (Steven Cheng [MSFT])
Organization: Microsoft
Date: Mon, 09 Jun 2008 03:53:34 GMT
Subject: Re: ADSI script implement in C#
>
Thanks for your reply John,

I'm not quite sure about a pure LDAP syntax query since AD is not my
speciality. But I'll try to perform some research and discuss with some
other AD engineers to see whether they can provide any further information
on this.

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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
>ications.
http://msdn.microsoft.com/subscripti...t/default.aspx.
================================================= =
This posting is provided "AS IS" with no warranties, and confers no
rights.--------------------
>>Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
<72**********************************@microsoft.c om>
<WM**************@TK2MSFTNGHUB02.phx.gbl>
>>Subject: Re: ADSI script implement in C#
Date: Thu, 5 Jun 2008 05:11:09 -0700
>>
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]" <st*****@online.microsoft.comwrote in message
news:WM**************@TK2MSFTNGHUB02.phx.gbl.. .
>>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/...s-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:
ms****@microsoft.com.

================================================ ==
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
>>ications.

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
Reply-To: "John" <pu****@hotmail.com>
From: "John" <pu********@community.nospam>
References: <A2**********************************@microsoft.co m>
<kh**************@TK2MSFTNGHUB02.phx.gbl>
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]" <st*****@online.microsoft.comwrote in message
news:kh**************@TK2MSFTNGHUB02.phx.gbl.. .
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:
ms****@microsoft.com.
>
============================================== ====
Get notification to my posts through email? Please refer to
>
http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
>>>>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/subscripti...t/default.aspx.
============================================== ====
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>Reply-To: "John" <pu****@hotmail.com>
>>From: "John" <pu********@community.nospam>
>>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
>>
>


Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.