Powershell 检查 OU 是否存在
我试图在创建 OU 之前检查它是否存在。我的问题是,我有 2 个母 OU“USER BY SITE”和“GROUP BY SITE”,并且我需要在这 2 个中拥有完全相同的 OU,其中 1 个用于存储用户,另一个用于存储组。
到目前为止我使用了这个函数:
function CheckOUExist
{
param($OUToSeek)
$LDAPPath = "LDAP://dc=Domain,dc=local"
$seek = [System.DirectoryServices.DirectorySearcher]$LDAPPath
$seek.Filter = “(&(name=$OUToSeek)(objectCategory=organizationalunit))”
$Result = $seek.FindOne()
return $Result
}
有一个问题,即使$LDAPPath =“OU=USERS BY SITE,DC=Domain,DC=local”,我总是得到“GROUP BY SITE”中存在的OU。我在那里错过了什么吗?有没有办法让 [System.DirectoryServices.DirectorySearcher] 仅在我在 $LDAPPath 中给出的 OU 中工作?
如果您需要更准确的详细信息,我很乐意提供。
先感谢您。
I'm trying to check if an OU exist before creating it. My problem is that I have 2 mother OU "USER BY SITE" and "GROUP BY SITE", and I need to have the exact same OU in those 2, 1 for storing users, the other for storing groups.
So far I used this function :
function CheckOUExist
{
param($OUToSeek)
$LDAPPath = "LDAP://dc=Domain,dc=local"
$seek = [System.DirectoryServices.DirectorySearcher]$LDAPPath
$seek.Filter = “(&(name=$OUToSeek)(objectCategory=organizationalunit))”
$Result = $seek.FindOne()
return $Result
}
There is my problem, I always get the OU existing in "GROUP BY SITE" even if $LDAPPath = "OU=USERS BY SITE,DC=Domain,DC=local". Am I missing something there? Is there a way to for the [System.DirectoryServices.DirectorySearcher] to work only in the OU I gived in the $LDAPPath?
If you need more accurate detail, I'll gladly provide them.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试 Exists 方法,您分别返回 true/false:
Try the Exists method, you get back true/false respectively:
如果您使用干净的数据,那么按照 Shay 的建议,以下内容非常有效。
感谢这个伟大的起点!但是,如果您正在验证可能不干净的数据,则会抛出错误。可能错误的一些示例是:
所有这些错误都应使用
[System.Management.Automation.RuntimeException]
捕获,或者您可以将 catch 语句留空以捕获全部。简单示例:
更多详细信息:
http://go.vertigion.com/PowerShell-CheckingOUExists
The following, as suggested by Shay, works great if you're working with clean data.
Thanks for this great starting point! However, if you're verifying potentially unclean data, you'll get thrown an error. Some examples of possible errors are:
All of these errors should be caught with
[System.Management.Automation.RuntimeException]
or you can just leave the catch statement blank to catch all.Quick Example:
More details:
http://go.vertigion.com/PowerShell-CheckingOUExists
问题在于 DirectorySearcher 对象的构造。要正确设置搜索根,需要使用 DirectoryEntry 对象([ADSI] 类型加速器)构造 DirectorySearcher,而您使用的是字符串。当使用字符串时,该字符串用作 LDAP 过滤器,并且搜索根为空,导致搜索器使用当前域的根。这就是为什么它看起来没有搜索您想要的 OU。
我想如果您执行如下操作,您将会得到您正在寻找的结果:
请注意,首先构造了一个 DirectoryEntry,然后使用它来构造 DirectorySearcher。
The problem is the construction of the DirectorySearcher object. To properly set the search root, the DirectorySearcher needs to be constructed using a DirectoryEntry object ([ADSI] type accelerator), whereas you are using a string. When a string is used, the string is used as the LDAP filter and the search root is null, causing the searcher to use the root of the current domain. That is why it looks like it isn't searching the OU you want.
I think you will get the results you are looking for if you do something like the following:
Notice that a DirectoryEntry is first constructed, which is then used to construct the DirectorySearcher.
怎么样:
既然
AD:
PSDrive 已加载,我们有几个选项:有关此主题的更多信息:玩转广告:驾驶乐趣和利润
How about:
Now that the
AD:
PSDrive is loaded, we have a couple of options:More info on this topic: Playing with the AD: Drive for Fun and Profit
也会工作
Will also work