Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

count the number of computers by OU

$
0
0

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.

  1. ##Created by Felipe Binotto 
  2. ##On the 20 of October of 2010 
  3.  
  4. <#   
  5.     .SYNOPSIS   
  6.         Count the number of computers in each OU recursivelly.   
  7.     .DESCRIPTION   
  8.         Display the number of computers in each OU recursivelly from the OU specified as parameter.  
  9.     .PARAMETER  code   
  10.         Specify OU when calling the script.  
  11.     .EXAMPLE   
  12.         CountOUMembers("Computers")  
  13.     
  14. #>   
  15.  
  16. function CountOUMembers([string]$OU){ 
  17.  
  18. $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() 
  19. $root = $dom.GetDirectoryEntry() 
  20.  
  21. $search = [System.DirectoryServices.DirectorySearcher]$root 
  22.  
  23.     $search.Filter = "(OU=$OU)" 
  24.     $result = $search.FindAll() 
  25.     $path = [ADSI]$result[0].path 
  26.     $children = $path.psbase.children 
  27.     $count = 0 
  28.          
  29.          
  30.         foreach($child in $children){ 
  31.              
  32.          
  33.             if($child.objectcategory -like '*organizational*'){ 
  34.      
  35.             CountOUMembers($child.name
  36.                                                         
  37.              
  38.             elseif($child.objectcategory -like '*computer*'){ 
  39.             [int]$count = $count + 1 
  40.              
  41.                 
  42.                  
  43.              
  44.             }         
  45.              
  46.             Write-Host "$count computers in $OU OU"         
  47.              
  48.                                         
  49.              
  50.              
  51. CountOUMembers($args[0]) 


Über Random


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>