Powershell检查是否存在OU

时间:2011-07-26 09:37:04

标签: powershell active-directory windows-server-2008-r2

我正在尝试在创建OU之前检查是否存在OU。我的问题是我有2个母亲OU“USER BY SITE”和“GROUP BY SITE”,我需要在那些2,1中存储完全相同的OU用于存储用户,另一个用于存储组。

到目前为止,我使用了这个功能:

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中工作?

如果您需要更准确的细节,我很乐意为您提供。

提前谢谢。

5 个答案:

答案 0 :(得分:12)

如Shay所建议的那样,如果你正在使用干净的数据,那么效果很好。

[string] $Path = 'OU=test,DC=domain,DC=com'
[adsi]::Exists("LDAP://$Path")

感谢这个伟大的起点!但是,如果您验证可能不清洁的数据,则会引发错误。可能的错误的一些示例是:

  • 如果某些内容格式不正确
    • (ERR:指定了无效的dn语法)
  • 如果域名不存在
    • (错误:服务器无法运行)
  • 如果域名不与您通信
    • (ERR:从服务器返回推荐)

所有这些错误都应该被[System.Management.Automation.RuntimeException]捕获,或者您可以将catch语句留空以捕获所有错误。

快速示例:

[string] $Path = 'OU=test,DC=domain,DC=com'
try {
    $ou_exists = [adsi]::Exists("LDAP://$Path")
} catch {
    # If invalid format, error is thrown.
    Throw("Supplied Path is invalid.`n$_")
}

if (-not $ou_exists) {
    Throw('Supplied Path does not exist.')
} else {
    Write-Debug "Path Exists:  $Path"
}

更多细节: http://go.vertigion.com/PowerShell-CheckingOUExists

答案 1 :(得分:11)

尝试Exists方法,分别返回true / false:

[adsi]::Exists("LDAP://OU=test,DC=domain,DC=com")

答案 2 :(得分:2)

问题是构建DirectorySearcher对象。要正确设置搜索根目录,需要使用DirectoryEntry对象([ADSI]类型加速器)构建DirectorySearcher,而使用字符串。使用字符串时,该字符串用作LDAP过滤器,搜索根为空,从而使搜索者使用当前域的根。这就是为什么看起来它没有搜索你想要的OU。

如果您执行以下操作,我认为您将获得所需的结果:

$searchroot = [adsi]"LDAP://OU=USERS BY SITE,DC=Domain,DC=local"

$seek = New-Object System.DirectoryServices.DirectorySearcher($searchroot)
$seek.Filter = "(&(name=$OUToSeek)(objectCategory=organizationalunit))"
... etc ...

请注意,首先构造一个DirectoryEntry,然后使用它构建DirectorySearcher。

答案 3 :(得分:1)

怎么样:

    Day      g1_mean     g2_mean   p.value
 1:   1  0.883406048  0.67177271 0.6674138
 2:   2  0.007544956 -0.55609722 0.3948459
 3:   3  0.409248637  0.28717183 0.8753213
 4:   4 -0.540075365  0.23181458 0.1785854
 5:   5 -0.632543900 -1.09965990 0.6457325
 6:   6 -0.083221671 -0.96286343 0.2011136
 7:   7 -0.044674252 -0.27666473 0.7079499
 8:   8  0.260795244 -0.15159164 0.4663712
 9:   9 -0.134164758  0.01136245 0.7992453
10:  10  0.496144329  0.76168408 0.1821123

现在已加载#Requires -Version 3.0 # Ensure the 'AD:' PSDrive is loaded. if (-not (Get-PSDrive -Name 'AD' -ErrorAction Ignore)) { Import-Module ActiveDirectory -ErrorAction Stop if (-not (Get-PSDrive -Name 'AD' -ErrorAction Silent)) { Throw [System.Management.Automation.DriveNotFoundException] "$($Error[0]) You're likely using an older version of Windows ($([System.Environment]::OSVersion.Version)) where the 'AD:' PSDrive isn't supported." } } PSDrive,我们有几个选项:

AD:

有关此主题的更多信息:Playing with the AD: Drive for Fun and Profit

答案 4 :(得分:-1)

Import-Module ActiveDirectory
Function CheckIfGroupExists{
    Param($Group)
    try{
        Get-ADGroup $Group
    }
    catch{
        New-ADGroup $Group -GroupScope Universal
    }
}

也会起作用

相关问题