我试图检查Get-Adcomputer返回的对象是否属于返回的DistinguishedName属性下的某个组。我似乎在网上找不到任何线索。这个属性包含几个不同的东西,我对OU值特别感兴趣。
$computer = Get-AdComputer "$computerName"
$computer.DistinguishedName这将返回几个链接到OU和CN的属性,但我只对其中一个OU属性与“Server”匹配感兴趣。
发布于 2016-06-23 21:17:03
以下是一些你可以尝试的东西,如果你只是在寻找一张真假支票
$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 ','注意:如果您的任何对象的名称中都有逗号,则这可能无法工作。
https://stackoverflow.com/questions/38001602
复制相似问题