Commands / Get-sqmServersFromOU

Get-sqmServersFromOU Configuration

Discovers SQL Server computers in an Active Directory Organizational Unit using pure ADSI/DirectorySearcher, no Active Directory PowerShell module required. Returns objects with a SqlInstance property that pipes directly into other sqmSQLTool functions.

Execution Flow

START Resolve domain: -Domain or [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name Build domainDN: DC=corp,DC=contoso,DC=com if -SearchBase → use directly; else DirectorySearcher (&(objectClass=organizationalUnit)(ou=<OUName>)) → FindOne() Target DN = OU result or provided -SearchBase DirectorySearcher: objectClass=computer, SearchRoot=<ouDN>, PageSize=1000 SearchScope: Subtree (if -Recurse) or OneLevel PropertiesToLoad: name, dnshostname, distinguishedname, operatingsystem, description FQDN from dnshostname (fallback: name + domain) OU extracted from first OU= segment of distinguishedname Build PSCustomObject: Name, FQDN, SqlInstance (=FQDN), OU, Domain, OperatingSystem, Description Write-Host progress; Write-Output object (pipeable) Return $computers[] (piped as emitted) DONE

Synopsis

Uses [System.DirectoryServices.DirectorySearcher] (ADSI) to locate computer objects under a named OU. No ActiveDirectory PowerShell module is required, works on any domain-joined machine. The returned objects have a SqlInstance property equal to the computer FQDN, which allows direct piping to any sqmSQLTool function that accepts -SqlInstance from the pipeline.

No dbatools required. No ActiveDirectory module required, pure .NET ADSI. Uses PageSize=1000 for LDAP paging. With -Recurse (default), searches sub-OUs as well. Returns objects immediately via Write-Output (streaming pipeline-compatible).

Syntax

Get-sqmServersFromOU
    [-OUName <String>]          # default 'srvDatabase'
    [-Domain <String>]          # default: current AD domain
    [-SearchBase <String>]      # explicit LDAP DN; overrides OUName lookup
    [-Recurse <Bool>]            # default $true

Parameters

ParameterTypeDefaultDescription
-OUNameStringsrvDatabaseOU name to locate via DirectorySearcher. Ignored when -SearchBase is specified.
-DomainStringcurrent AD domainAD domain name. Auto-detected from the machine's domain membership.
-SearchBaseString, Explicit LDAP DN to use as search root, e.g. OU=srvDatabase,DC=corp,DC=contoso,DC=com.
-RecurseBool$trueSearch all sub-OUs (Subtree) or only direct children (OneLevel).
-EnableExceptionswitch$falseLoest bei Fehlern sofort eine Exception aus.

Return Value

PropertyDescription
NameNetBIOS computer name (AD name attribute).
FQDNFully qualified DNS name (dnshostname).
SqlInstanceAlias of FQDN, enables direct piping to sqmSQLTool functions.
OUParent OU name extracted from distinguishedname.
DomainAD domain name.
OperatingSystemOS name from AD computer object.
DescriptionComputer description from AD.

Examples

Example 1, List all computers in the default 'srvDatabase' OU

Get-sqmServersFromOU

Example 2, Custom OU name, different domain

Get-sqmServersFromOU -OUName "SQL_Servers" -Domain "eu.contoso.com"

Example 3, Pipeline to disk space report across all SQL servers in OU

Get-sqmServersFromOU | Get-sqmDiskSpaceReport -ContinueOnError

Example 4, Explicit DN for a deep OU

Get-sqmServersFromOU -SearchBase "OU=Prod,OU=SQL,DC=corp,DC=contoso,DC=com"