Get-sthLDAPComputersByOperatingSystem.ps1

<#
.synopsis
Function for getting all Enabled computer objects from Active Directory using ADSI
and grouping them by Operating System using custom object properties:
XP, Seven, Eight and Ten.
 
.description
Function queries current domain naming context of Active Directory using ADSI
and returns custom object that contains Enabled computer objects
grouped by Operating System.
 
This object's contents can be used as input for cmdlets and functions that influence
multiple computers and take computer names or other information as parameters' values.
 
Returned custom object consists of four main properties: XP, Seven, Eight and Ten.
These properties contain objects representing computers with all the properties,
that was returned by ADSI query.
 
 
You can use this function as:
 
$Comps = Get-sthLDAPComputersByOperatingSystem
 
Than you can get all "Windows XP" computers by typing:
$Comps.XP
 
"Windows 7" computers:
$Comps.Seven
 
"Windows 8" and "Windows 8.1" computers:
$Comps.Eight
 
"Windows 10" computers:
$Comps.Ten
 
By default these computers objects show properties
that Get-ADComputer cmdlet without -Properties parameter would show.
You can get all computer properties as: $Comps.Ten | Format-List *
 
 
Also this custom object contains two additional properties,
that is created by means of types file: All and Summary.
 
All property contains all computer objects from four main properties:
XP, Seven, Eight and Ten, so that you can reference all computers at once.
 
Summary property contains here-string that consists of names of all computers
from four main properties.
This property is used by default custom view, defined in format file.
 
.example
$Comps = Get-sthLDAPComputersByOperatingSystem
$Comps
 
Displays summary information about all enabled computer objects,
returned by query.
 
.example
$Comps = Get-sthLDAPComputersByOperatingSystem
$Comps.Ten
 
Displays "Windows 10" computer objects.
By default it shows only DistinguishedName, DNSHostName, Name, ObjectClass,
GUID, SamAccountName and SID properties.
 
.example
$Comps = Get-sthLDAPComputersByOperatingSystem
$Comps.Ten | Format-List *
 
Displays "Windows 10" computer objects.
By specifying "*" as Format-List cmdlet parameter command returns
all computer objects' properties.
 
.example
$Comps = Get-sthLDAPComputersByOperatingSystem
$Comps.Ten.Name
 
Displays names of all enabled "Windows 10" computers.
 
.example
$Comps = Get-sthLDAPComputersByOperatingSystem
$Comps.Ten.Name | Test-sthWindowsUpdateRebootRequired
 
Tests if some of the "Windows 10" computers requre reboot after installing updates.
 
#>


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
}