Powershell用于查询域上的特定OU

时间:2017-06-29 13:33:45

标签: powershell active-directory

Function    ListComputers
            {
                $strCategory = "computer"
                $objDomain = New-Object System.DirectoryServices.DirectoryEntry
                $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
                $objSearcher.SearchRoot = $objDomain
                Write-Host $objDomain
                $objSearcher.Filter = ("(objectCategory=$strCategory)")
                $colProplist = "name"
                foreach ($i in $colPropList)
                {
                    $objSearcher.PropertiesToLoad.Add($i)
                }
                $colResults = $objSearcher.FindAll()

                foreach ($objResult in $colResults)
                {
                    $objComputer = $objResult.Properties; $objComputer.name
                }
            }

这是在搜索整个域名。如何仅搜索域中的特定OU?

2 个答案:

答案 0 :(得分:1)

您可以使用:

Get-ADComputer -Filter * -SearchBase "CN=Workstations, DC=contoso, DC=com"

它可能会让它更容易一点吗?

答案 1 :(得分:0)

有几种不同的方式。您可以使用Get-ADOrganizationalUnit

Get-ADOrganizationalUnit -Identity 'OU=Location,OU=Group,OU=UserAccounts,DC=STUFF,DC=COM'

否则您仍然可以使用Get-ADComputer

Get-ADComputer -Filter '*' -SearchBase "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"

在此处找到第二个示例:Powershell script to query specifc OU in AD for computer names and export