I am using the script listed below (from: http://powershell.com/cs/media/p/7911.aspx) and it works pretty well for about half of the directory. However we have separate OU's with the same name"Athletics" in our directory and when the script hits the first one it gets stuck and just keeps repeating "73 computers in Athletics OU". Can anyone tell what is breaking it and how to fix it? Thanks.
- ##Created by Felipe Binotto
- ##On the 20 of October of 2010
- <#
- .SYNOPSIS
- Count the number of computers in each OU recursivelly.
- .DESCRIPTION
- Display the number of computers in each OU recursivelly from the OU specified as parameter.
- .PARAMETER code
- Specify OU when calling the script.
- .EXAMPLE
- CountOUMembers("Computers")
- #>
- function CountOUMembers([string]$OU){
- $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
- $root = $dom.GetDirectoryEntry()
- $search = [System.DirectoryServices.DirectorySearcher]$root
- $search.Filter = "(OU=$OU)"
- $result = $search.FindAll()
- $path = [ADSI]$result[0].path
- $children = $path.psbase.children
- $count = 0
- foreach($child in $children){
- if($child.objectcategory -like '*organizational*'){
- CountOUMembers($child.name)
- }
- elseif($child.objectcategory -like '*computer*'){
- [int]$count = $count + 1
- }
- }
- Write-Host "$count computers in $OU OU"
- }
- CountOUMembers($args[0])
Über Random