Gather AD Data For CMDB True Up

Required PowerShell Modules:

  • ActiveDirectory: This module is installed when you install the Remote Server Administration Tools (RSAT). As of Windows 10 RSAT is installed as a optional feature.

How you go about selecting computer objects from AD for inclusion in your CMDB true up activity is dependent on what your AD architecture is and what your OU structure is. For this narrative, my environment requires two distinct SearchBases, $srchBase1 and $srchBase2. Use the SearchBase an on OS filter to get only the server computer objects within the correct scope for the CMDB.

# Start setup for gathering server AD data
# Array to hold the computer objects
$adSvrs = @()
# Filter out all the client operating systems
$OsFilter = "(OperatingSystem -notlike 'Windows 10*') -and (OperatingSystem -notlike 'Windows xp*') -and (OperatingSystem -notlike 'Windows 8.1*') -and (OperatingSystem -notlike 'Windows 7*')"
# Get subset of all available attributes. Will use DNSHostname to match
# with the CMDB data
$select = 'Name','DNSHostName','IPv4Address','OperatingSystem','Distinguishedname','managedby','PasswordLastSet','enabled','Description','MemberOf'
# set the SearchBase's
$srchBaseArray = @()
$srchBase1 = 'OU=...' #based on your AD OU structure and domain
$srchBase2 = 'OU=...' #based on your AD OU structure and domain
$srchBaseArray += $srchBase1
$srchBaseArray += $srchBase2
# End setup for gathering server AD data

Ready to get the server objects from AD.

# Start get server ad objects
Foreach ($sb in #srchBaseArray) {
   $adSvrs += Get-ADComputer -filter $osfilter  -property * -SearchBase $sb | Select-Object $select
}
$adCount = $adSvrs.Count

$adOutString = "Have gotten $adCount server objects from AD"
Write-EventLog -LogName Application -Source "CustomScripts" -EventId 1000 -EntryType Information $adOutString
# End get the server objects from AD.