The Iterator of the Endless AD Forest
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Sysadmin Telepathy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// POWERSHELL PATTERN //
class IIterator {
[bool] HasNext() { throw "Not Implemented" }
[object] Next() { throw "Not Implemented" }
}
class ADUserCollection {
[string[]]$_users
ADUserCollection([string[]]$u) { $this._users = $u }
[IIterator] CreateIterator() { return [ADUserIterator]::new($this) }
}
class ADUserIterator : IIterator {
[ADUserCollection]$_collection
[int]$_index = 0
ADUserIterator([ADUserCollection]$coll) { $this._collection = $coll }
[bool] HasNext() {
return $this._index -lt $this._collection._users.Length
}
[object] Next() {
$user = $this._collection._users[$this._index]
$this._index++
return $user
}
}
# Traversing the forest
$users = [ADUserCollection]::new(@("Administrator", "Guest", "Krbtgt"))
$iterator = $users.CreateIterator()
while ($iterator.HasNext()) {
Write-Host "Scrying user: $($iterator.Next())"
}
Navigating the shadowy woods of Active Directory requires focus. The Iterator abstracts the traversal of complex topological graphs, yielding entities one by one without revealing the terrifying depth of the forest.
Runic Tags:
#powershell
#sysadmin
#behavioral
#iterator
#active-directory