windows 使用 Powershell 获取嵌套的 OU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6407783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:06:47  来源:igfitidea点击:

Get Nested OU's using Powershell

windowsarrayspowershell

提问by PowerShell

Im trying to write a powershell script to get an OU information for servers which are in nested OU without using QAD cmdlets, i was helped by one stack member to write a code as below

我正在尝试编写一个 powershell 脚本来获取嵌套 OU 中的服务器的 OU 信息,而不使用 QAD cmdlet,我在一个堆栈成员的帮助下编写了如下代码

$computerName = "DC1"
$found = $FALSE
$domain = [ADSI]("LDAP://dc=contoso,dc=com")

$ous = ($domain.psbase.children |
        Where-Object {$_.psBase.schemaClassName -eq "OrganizationalUnit"} |
        Select-Object -expand Name)        

foreach ($child in $ous){
    $ou = [ADSI]("LDAP://ou=$child,dc=contoso,dc=com")
    $computers = ($ou.psbase.children |
                  Where-Object {$_.psBase.schemaClassName -eq "Computer"} |
                  Select-Object -expand Name)

    foreach ($client in $computers){
        if ($client -eq $computerName) {
            Write-Host "Found $computerName in" $ou.psBase.name
            $found = $TRUE
        }
    }
}

if (-not $found) {Write-Host "$computerName not found."}

I wanted some help in modifictaion of the same to seacj a computer's existence in a nested OU.

我需要一些帮助来修改相同的内容以确保计算机在嵌套 OU 中的存在。

Thanks, Vinith

谢谢,维尼斯

回答by Shay Levy

You can use the adsisearcher accelerator:

您可以使用 adsisearcher 加速器:

$searcher = [adsisearcher]'(&(ObjectCategory=computer)(Name=DC1))'
$searcher.FindOne()

回答by Brad

#  Reference -- http://powergui.org/thread.jspa?threadID=17534
#  List and count user objects per OU 

Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
cd\
cls

$File = "C:\Scripts\CountComputersInOu.csv"
#To specify parent OU "your.domain.com/computer"
$StartOU = "your.domain.com/"
$strFilter = ‘(objectClass=Computer)'

foreach ($targetou in Get-QADObject -SearchRoot $StartOU -Type organizationalUnit)
{

$Parent = [ADSI]"LDAP://$targetou"
#"These are the users in $($Parent.PSBase.Parent.Name): " | Out-File $File -append

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = 'LDAP://'+$targetou+''
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "onelevel"

$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()


"There are $($colResults.count) active computers in $($Parent.PSBase.Parent.Name)$($targetou.name)
" | Out-File $File -append
}