[英]C# Active Directory Search
I have this powershell function and i want to make it as a C# function.我有这个 powershell 函数,我想把它作为一个 C# 函数。 How can i put it into C#?我怎样才能把它放到 C# 中?
Get-ADComputer -filter {Name -Like 'myComp'} -property * | select DistinguishedName
You should be able to do this quite easily.你应该能够很容易地做到这一点。 Add a reference to System.DirectoryServices.AccountManagement
and then use this code:添加对System.DirectoryServices.AccountManagement
的引用,然后使用以下代码:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 'YourDomain'))
{
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity (ctx, "name");
if (computer != null)
{
// do whatever you need to do with your computer principal
string distinguishedName = computer.DistinguishedName;
}
}
Update : if you don't know your domain ........ - you can also use:更新:如果您不知道您的域名........ - 您也可以使用:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
in which case the principal context is created for the current domain you're located in.在这种情况下,将为您所在的当前域创建主体上下文。
You can use C# in the following manner您可以通过以下方式使用 C#
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName)
{
using (PrincipalSearcher srch = new PrincipalSearcher(new ComputerPrincipal(ctx) { Name = "ServerName"}))
{
return srch.FindAll().Cast<ComputerPrincipal>().ToList().Select(x => x.DistinguishedName);
}
}
Above returns a list of DistinguishedNames that matches the Server Name.以上返回与服务器名称匹配的 DistinguishedNames 列表。
All the other answers suggest using the System.DirectoryServices.AccountManagement
namespace.所有其他答案都建议使用System.DirectoryServices.AccountManagement
命名空间。 While that will work, it is really just a wrapper around the System.DirectoryServices
namespace to make things a bit easier to use.虽然这会起作用,但它实际上只是System.DirectoryServices
命名空间的一个包装器,使事情更容易使用。 It does make things easier (sometimes) but it does so at the cost of performance.它确实让事情变得更容易(有时),但这样做是以牺牲性能为代价的。
For example, in all the examples you've been given, your code will retrieve every attribute with a value from the computer object in AD, even though you only want one attribute.例如,在您获得的所有示例中,您的代码将从 AD 中的计算机对象中检索每个具有值的属性,即使您只需要一个属性。
If you use DirectorySearcher
, you can make the search and retrieve only that one attribute that you want:如果您使用DirectorySearcher
,您可以进行搜索并仅检索您想要的一个属性:
public string GetComputerDn(string computerName) {
var searcher = new DirectorySearcher {
Filter = $"(&(objectClass=computer)(sAMAccountName={computerName}$))",
PropertiesToLoad = { "distinguishedName" } //return only the distinguishedName attribute
};
var result = searcher.FindOne();
if (result == null) return null;
return (string) result.Properties["distinguishedName"][0];
}
Note that in AD, the sAMAccountName
of computer objects is what you would normally refer to as the "computer name", followed by $
, which is why the filter is what it is.请注意,在 AD 中,计算机对象的sAMAccountName
是您通常所说的“计算机名称”,后跟$
,这就是过滤器就是它的原因。
Please try this:请试试这个:
Add reference to Active Directory Services (%programfiles%\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\\\System.DirectoryServices.AccountManagement.dll)添加对 Active Directory 服务的引用 (%programfiles%\\Reference Assemblies\\Microsoft\\Framework.NETFramework\\\\System.DirectoryServices.AccountManagement.dll)
public string GetComputerName(string computerName)
{
using (var context = new PrincipalContext(ContextType.Domain, "your domain name goes here"))
{
using (var group = GroupPrincipal.FindByIdentity(context, "Active Directory Group Name goes here"))
{
var computers = @group.GetMembers(true);
return computers.FirstOrDefault(c => c.Name == computerName).DistinguishedName;
}
}
return null; // or return "Not Found"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.