Get-sthLDAPComputersByOperatingSystem.ps1

<#
.externalhelp sthTools.psm1-Help.xml
#>


function Get-sthLDAPComputersByOperatingSystem
{
    Param()

    $os = [ordered]@{
        XP = '(&(objectClass=computer)(OperatingSystem=Windows XP*)(!userAccountControl:1.2.840.113556.1.4.803:=2))' 
        Seven = '(&(objectClass=computer)(OperatingSystem=Windows 7*)(!userAccountControl:1.2.840.113556.1.4.803:=2))' 
        Eight = '(&(objectClass=computer)(OperatingSystem=Windows 8*)(!userAccountControl:1.2.840.113556.1.4.803:=2))'
        Ten = '(&(objectClass=computer)(OperatingSystem=Windows 10*)(!userAccountControl:1.2.840.113556.1.4.803:=2))' 
    }

    $RootDSE = [ADSI]"LDAP://RootDSE"
    $NC = $RootDSE.defaultNamingContext
    $SearchRoot = 'LDAP://' + $NC

    $Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher
    $Searcher.SearchRoot = $SearchRoot

    $ComputersByOperatingSystem = @{}

    foreach ($osname in $os.Keys)
    {
        $Searcher.Filter = $os["$osname"]
        $SearchResult = $Searcher.FindAll()

        $CompsFamily = @()
        $CompsFamily = foreach ($s in $($SearchResult | Sort-Object -Property Path))
        {
            $Comp = @{}
            $s.Properties.GetEnumerator() | ForEach-Object -Process {$Comp.Add($PSItem.Key, $($PSItem.Value))}
            [PSCustomObject]$Comp | Add-Member -TypeName sth.Computer -PassThru
        }
        
        $ComputersByOperatingSystem.Add($osname, $CompsFamily)
    }

    [PSCustomObject]$ComputersByOperatingSystem | Add-Member -TypeName sth.ComputersByOperatingSystem -PassThru
}