简体   繁体   English

Powershell AD对象DistinguishedName属性

[英]Powershell AD-Object DistinguishedName attributes

I am trying to check if an object returned by the Get-Adcomputer belongs to a certain group under the DistinguishedName property returned. 我正在尝试检查Get-Adcomputer返回的对象Get-Adcomputer属于返回的DistinguishedName属性下的某个组。 I can't seem to find anything online and looking for some pointers. 我似乎无法在网上找到任何东西并寻找一些指示。 This attribute contains several different things and I am interested in the OU value in particular. 该属性包含一些不同的内容,我特别对OU值感兴趣。

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

This returns several attributes linked to OU and CN, but I am just interested if one of the OU attributes matches 'Server'. 这将返回链接到OU和CN的几个属性,但是我只想知道OU属性之一是否与“服务器”匹配。

here is something you could try if you're just looking for a true/false check 如果您只是在寻找真假检查,可以尝试以下方法

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

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

or you could use this to get the whole path 或者您可以使用它来获取整个路径

# 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 ','

note: this will likely not work if any of your objects have a comma in the name. 注意:如果您的任何对象名称中都有逗号,则可能无法使用。

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

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