PowerShell: Retrieve List of SCCM Site Codes

If you’re using System Center Configuration Manager (SCCM / ConfigMgr) 2007, you may want to discover how many SCCM sites you have from Active Directory. Of course, this assumes that you have Active Directory publishing enabled on your primary sites. When enabled, SCCM automatically places site information underneath the CN=System Management,CN=System,DN=mydomain,DC=com container.

Here is some simple PowerShell code to enumerate the site codes of all the SCCM site codes that Active Directory is aware of. This can be useful information for troubleshooting issues related to client location services (eg. a SCCM client that cannot auto-discover its site code).

# (optional) Dynamically retrieve the domain root distinguished name
$DomainRoot = ([adsi]"").distinguishedName

# (optional) Get ADSI object to represent search root
$SearchRoot = [adsi]("LDAP://CN=System Management,CN=System," + $DomainRoot)

# Create an ADSI searcher object, with LDAP-style search filter syntax
$AdSearcher = [adsisearcher]"(objectClass=mSSMSSite)"

# (optional) Set search root of Active Directory searcher to System Management container
$AdSearcher.SearchRoot = $SearchRoot

# Make call to retrieve objects from Active Directory
$SccmSites = $AdSearcher.FindAll()

# Iterate over each mSSMSSite object, and give me the mssmssitecode property
# IMPORTANT: All Active Directory property references must be in lower-case, else ... fail
foreach ($SccmSite in $SccmSites)