简体   繁体   English

使用Powershell获取嵌套OU

[英]Get Nested OU's using 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 谢谢,Vinith

You can use the adsisearcher accelerator: 您可以使用adsisearcher加速器:

$searcher = [adsisearcher]'(&(ObjectCategory=computer)(Name=DC1))'
$searcher.FindOne()
#  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
}

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

相关问题 Powershell 获取OU中所有服务器的lastlogonDate - Powershell get lastlogonDate of all servers in OU 在Powershell中与USER OU交互 - Interacting with USER OU in Powershell 尝试使用调用命令获取与 OU 相关的所有 GPO - trying to get all the GPO's related to the OU with invoke command 调用命令错误,尝试通过脚本从 dc 的特定 OU 获取链接的 GPO - Invoke command error, trying to get linked GPO's from specific OU from dc by script 有没有办法使用 ou 规范名称将 GPO 与 ou 联系起来? - Is there any way linking GPO with ou using the ou canonical name? Powershell 脚本必须以人类可读的格式显示所有工作站 - Powershell script has to show all workstations ou in human readable format 如何使用 jsonpath 在 Windows Powershell 的每一行上获取 k8s 集群节点名称? - How to get the k8s cluster node names one name on each line on Windows Powershell using jsonpath? 获取所有包含相同名称 OU=NEEDOU 的 OU 的用户。 从某个数据库 - Get all users that consist bunch of OU with the same names OU=NEEDOU. From a certain database Powershell使用scanf进行编程的管道 - Powershell's pipe to program using scanf 如何在此命令中列出不同的OU:Get-Mailbox -OrganizationalUnit - How to list different OU in this command: Get-Mailbox -OrganizationalUnit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM