简体   繁体   English

如何使用WMI获取计算机的当前OU并列出该OU中的所有其他计算机?

[英]How do I use WMI to get the current OU of a computer and list all other computers in that OU?

我正在使用WMI,我正在尝试找到一个powershell脚本,它允许我获取本地计算机的OU,然后获取该OU中的完整计算机列表。

Here you go: 干得好:

$ComputerName = '<Name of Computer>';
$Computer = Get-WmiObject -Namespace 'root\directory\ldap' -Query "Select DS_distinguishedName from DS_computer where DS_cn = '$ComputerName'";
$OU = $Computer.DS_distinguishedName.Substring($Computer.DS_distinguishedName.IndexOf('OU='));
$ComputersInOU = Get-WmiObject -Namespace 'root\directory\ldap' -Query "Select DS_cn, DS_distinguishedName from DS_computer where DS_distinguishedName like '%$OU'";

I think this also finds computers in child OUs, but I'm not sure how to limit it to a single OU without doing a ton of queries. 我认为这也可以在子OU中找到计算机,但我不知道如何在不进行大量查询的情况下将其限制为单个OU。 The query syntax is just rather sparse. 查询语法相当稀疏。 Eliminating the child OU objects after retrieval of the full list might be the only way to do it with any semblance of performance. 在检索完整列表之后消除子OU对象可能是执行任何表现的唯一方法。

Fair warning: This is slow. 公平警告:这很慢。 Really slow. 真的很慢。 Like "Oh, crap did I break something?!" 就像“哦,垃圾,我打破了什么?!” slow. 慢。 I pointed it at a computer that shares an OU with less than 20 other computers, and it takes nearly a minute to run. 我把它指向一台与少于20台其他计算机共享OU的计算机,运行起来需要将近一分钟。 Even the first fetching of just the single computer takes more than 1 second. 即使是第一次只读取一台计算机也需要1秒钟以上。

Here's what I would recommend: 这是我建议的:

$ComputerName = '<Name of Computer>';
Import-Module -Name ActiveDirectory -Cmdlet Get-ADComputer, Get-ADOrganizationalUnit;
$Computer = Get-ADComputer $ComputerName;
$OU = $Computer.DistinguishedName.SubString($Computer.DistinguishedName.IndexOf('OU='));
$ComputersInOU = Get-ADComputer -Filter * -SearchScope OneLevel -SearchBase (Get-ADOrganizationalUnit $OU).DistinguishedName;

That takes 2 seconds, including loading the Active Directory module. 这需要2秒,包括加载Active Directory模块。 With it already loaded, that takes less than 200 milliseconds. 已经加载,这需要不到200毫秒。

If you don't have access to the ActiveDirectory PowerShell module, then you can use an [ADSISearcher] . 如果您无权访问ActiveDirectory PowerShell模块,则可以使用[ADSISearcher] These are also a pain to use because of how results are presented, but they're even faster than the ActiveDirectory module, which is basically just a wrapper for this. 由于结果如何呈现,这些也很难使用,但它们甚至比ActiveDirectory模块更快,它基本上只是一个包装器。

$ComputerName = '<Name of Computer>';
$ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher;
$ADSISearcher.Filter = '(&(name=' + $ComputerName + ')(objectClass=computer))';
$ADSISearcher.SearchScope = 'Subtree';
$Computer = $ADSISearcher.FindAll();

$OU = $($Computer.Properties.Item('distinguishedName')).Substring($($Computer.Properties.Item('distinguishedName')).IndexOf('OU='));
$OUADsPath = 'LDAP://' + $OU;

$ADSISearcher = New-Object System.DirectoryServices.DirectorySearcher;
$ADSISearcher.Filter = '(objectClass=computer)';
$ADSISearcher.SearchScope = 'OneLevel';
$ADSISearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry($OUADsPath);
$ComputersInOU = $ADSISearcher.FindAll();

This runs in about 50 milliseconds. 这运行大约50毫秒。

However, beware that the ADSI system is known to contain memory leaks if it's not called correctly or if FindAll() is called and the results are never used. 但是,请注意,如果ADSI系统未正确调用或者调用FindAll()并且结果从未使用过, 则已知ADSI系统包含内存泄漏 I myself have created objects with this method and then not disposed of them and left my shell process open overnight, and when I came in the next morning my system was all but unresponsive because all the memory had been consumed. 我自己用这种方法创建了对象然后没有丢弃它们并且让我的shell进程在一夜之间打开,当我第二天早上进入时,我的系统几乎没有响应,因为所有的内存都被消耗了。 The ActiveDirectory module avoids these problems entirely, and is even more code light, so unless you really need those extra few milliseconds than I would favor that module. ActiveDirectory模块完全避免了这些问题,并且代码更轻,所以除非你真的需要那些额外的几毫秒而不是我喜欢的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM