Powershell AD-Object DistinguishedName属性

时间:2016-06-23 20:49:33

标签: powershell active-directory

我正在尝试检查Get-Adcomputer返回的对象是否属于返回的DistinguishedName属性下的某个组。我似乎无法在网上找到任何东西,并寻找一些指针。此属性包含几个不同的内容,我特别感兴趣的是OU值。

$computer = Get-AdComputer "$computerName"
$computer.DistinguishedName

这会返回链接到OU和CN的几个属性,但我只对其中一个OU属性与“Server”匹配感兴趣。

1 个答案:

答案 0 :(得分:0)

如果您只是在寻找真/假检查,那么您可以尝试一下

$computer = Get-AdComputer "$computerName"
$dn = $computer.DistinguishedName

$dn.Split(',') -match '^ou=.*server'

或者您可以使用它来获取整个路径

# split the distinguishedname into an array of CNs, OUs, etc.
$split = $dn.Split(',')

# setup match variable to signal that we found an object that matches what we are looking for.
$match = $false

# for each item in the array
($split | % {
    # only check this if we have not yet found a match
    # if this line is an OU whose name contains the word 'server'
    if (!$match -and $_ -match '^ou=.*server') {
        # set match to true because we found what we were looking for
        $match = $true
    }
    # if we have found a match, output this and all the rest of the objects to the pipeline
    if ($match) {
        $_
    }
# now we have an array starting at the OU we were looking for
# this join will put the pieces back together into a string so it looks right
}) -join ','

注意:如果您的任何对象名称中包含逗号,则可能无效。