在活动目录中查找计算机 [英] Find computers in active directory

查看:135
本文介绍了在活动目录中查找计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用dsa.msc手动搜索计算机并打开其属性时,会出现一个位置标签。它可能有也可能没有值。
当我尝试使用.Net的目录服务获取此信息时,没有看到 location属性。我打印了所有可用的属性,但没有看到它。只是不可用还是我错过了什么?
这是部分代码:

When I manually search for a computer using dsa.msc and open its properties, there is a "Location" tab. It might or might not have a value in it. When I try to get this information using directory services of .Net, I do not see a "location" property. I printed out all available properties and don't see it. Is it just not available or am I missing something? This is partial code:

string sADPath = "LDAP://blah.blah.com";
DirectoryEntry de = new DirectoryEntry(sADPath);

string sFilter = "(&(objectCategory=computer)(name=" + sComputerName + "))";
DirectorySearcher DirectorySearch = new DirectorySearcher(de, sFilter);
SearchResult DirectorySearchResult = DirectorySearch.FindOne();

if (null != DirectorySearchResult)
{
    DirectoryEntry deComp = DirectorySearchResult.GetDirectoryEntry();
    oComputer.CN = deComp.Properties["cn"].Value.ToString();
    ....
}






编辑:

我误解了要求!它不是我需要的计算机的物理位置,而是在AD层次结构中的位置。似乎没有位于 abc.org-> A-> B中的计算机,但位于 abc.org-> A-> C-> D中。我需要的是能够找到给定计算机名称的路径 abc.org-> A-> C-> D。

I misunderstood the requirment! It is not the "physical" location of the computer I need but where in the AD hierarchy it is. It seems that a computer that is supposed to be in "abc.org --> A --> B" is not there but is located in "abc.org --> A --> C --> D". What I need is to be able to find the path "abc.org --> A --> C --> D" given a computer name.

推荐答案

属性名称为位置。与所有广告属性一样,如果搜索结果对象没有值,则不会在搜索结果对象上看到它。我已经弄弄了您的代码,以便它可以在我的机器上正常工作。

The attribute name is 'location'. As with all AD properties, you won't see it on a search result object if it doesn't have a value. I've fiddled with your code so that it would just work on my machine.

如果您只是在检索数据而又不打算进行任何更改,则不会需要调用GetDirectoryEntry(这将使服务器再次往返)。请注意语法上的细微差别:

If you're only retrieving data and not planning to make any changes you don't need to call GetDirectoryEntry (which will impose another round-trip to the servers). Note the slight difference in syntax:

var rootDSE = new DirectoryEntry("LDAP://RootDSE");
var defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
var domainRootADsPath = String.Format("LDAP://{0}", defaultNamingContext);
var searchRoot = new DirectoryEntry(domainRootADsPath);

var filter = "(&(objectCategory=computer)(name=" + computerName + "))";
var directorySearch = new DirectorySearcher(searchRoot, filter);
var directorySearchResult = directorySearch.FindOne();

if (null != directorySearchResult)
{
    Console.WriteLine(directorySearchResult.Properties["cn"][0].ToString());
    if (directorySearchResult.Properties["location"].Count > 0)
    {
        Console.WriteLine(directorySearchResult.Properties["location"][0].ToString());
    }

    //It's not necessary to run GetDirectoryEntry unless you want to make a change
    DirectoryEntry deComp = directorySearchResult.GetDirectoryEntry();
    Console.WriteLine(deComp.Properties["cn"].Value.ToString());
    if (deComp.Properties["location"].Value != null)
    {
        Console.WriteLine(deComp.Properties["location"].Value.ToString());
    }
}

这篇关于在活动目录中查找计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆