473,226 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ADSI script implement in C#

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
8 3068
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sophia | last post by:
I need to authenticate users to enter in a NTLM-protected virtual directory, but I can't pop up a NT-login dialogue box - I can only do a web-based username/password form (my client is a...
4
by: Akhlaq Khan | last post by:
we are developing an intranet application (web based) which needs to detect the logged in user ID of the user hitting the website. the intranet is huge and based on win2k active directory (around...
0
by: Andrew Jocelyn | last post by:
Hi I'd like to be able to list IIS websites and information in a web page (presumably using ADSI). Similar to the list you get in the MMC plug-in. Does anyone have a simple script I could start...
2
by: vorpal | last post by:
Hi, I have successfully set and used a linked server to query ADSI. Since this question also concerns MSSQLServer, I've cross posted it -- I hope this is not a breach of etiquette. I have...
14
by: Arran Pearce | last post by:
Hi, I am looking for a way to use System.DirectoryServices to find all users on a domain whos accounts are either locked out or disabled. I have used ADSIEdit and the mmc schema add-in to try...
3
by: chat_devil | last post by:
hi, does anyone know if it is possible to remove an attribute that can not be read into the ADSI property cache/collection. i'm trying to do an eDirectory password change from .net directory...
0
by: LamSoft | last post by:
I found that getting values using ADSI From other 2003 server-based is very slow, if the script (ASP.NET) is also running on Windows 2003 Server. But when I develop the script on my Windows XP on...
3
by: TomikZ | last post by:
I'd like to get some user account properties using .asp script. When I try to use ADSI WINNT provider it works fine except that I apparently cannot access some properties such as...
1
by: zmunoz | last post by:
I am working on a script using ADSI via COM in PHP. The script works for the most part when I query a user using the following function: function passwordExpires($userDN) { $comLDAP = new...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.