开发者

Get Nested OU's using Powershell

开发者 https://www.devze.com 2023-03-14 05:44 出处:网络
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

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

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

$ous = ($domain.psbase.children |
        Where-Object {$_.psBase.schemaClassName -eq "Organizationa开发者_开发问答lUnit"} |
        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.

Thanks, Vinith


You can use the adsisearcher accelerator:

$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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消