PoshCode Archive  Artifact Content

Artifact 5eb6ad9e3649594e5f83aad46823777dc7b88a05c3954daad122d5b8d77f1b15:

  • File Servers-in-Computers-OU.ps1 — part of check-in [ef8665ab81] at 2018-06-10 14:25:04 on branch trunk — Many organizations have a separate OUs for their servers and workstations, this is an example on how to list servers in the wrong OU. (user: unknown size: 1483)

# encoding: ascii
# api: powershell
# title: Servers in Computers OU
# description: Many organizations have a separate OUs for their servers and workstations, this is an example on how to list servers in the wrong OU.
# version: 0.1
# type: function
# license: CC0
# x-poshcode-id: 848
# x-archived: 2009-02-26T17:45:20
#
# This will list any Entries with “Server“ in OS name in the Computers OU in ActiveDirectory.
#
function checkADSI()
{ $ADSIStat = "Servers in Computer OU"
  $error.clear()
  $errcnt = 0
    
  $objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://CN=Computers,DC=domain,DC=com")
  $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomain)
  $objSearcher.Filter = ("(operatingSystem=*Server*)")
  $objSearcher.SearchScope = "OneLevel"
  
  $colProplist = "dnshostname"
  foreach ($i in $colPropList)
  {$idx = $objSearcher.PropertiesToLoad.Add($i)}

  $colResults = $objSearcher.FindAll()
  for($i = 0; $i -lt ($colResults.count -1); $i++ ) 
  { $objResult = $colResults[$i]
    $objComputer = $objResult.Properties
    $cName = ($objComputer.dnshostname -replace ".domain.com","")
    
    $ADSIStat += "`n`t! " + $cname
    $errcnt += 1
    
  }
  if($error[0])
  { $ADSIStat += "`n`t Errors Occured: "
    foreach($err in $error)
    { $ADSIStat += "`n`t`!" + $err }
  } 
  elseif($errCnt -eq 0)
  {$ADSIStat += " None" }
  
  return $ADSIStat
}#function checkADSI()