Files
popki/resources/tasks/windows/ad-inventory.ps1
T

45 lines
2.8 KiB
PowerShell

# Fixed read-only AD DC inventory; no credentials. See manifest and adapter contract.
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
Import-Module ActiveDirectory -ErrorAction Stop
$node = Get-CimInstance Win32_ComputerSystem
if ($node.DomainRole -lt 4) { throw 'Select the source AD domain controller.' }
$domain = Get-ADDomain -Server $node.Name
if ($domain.DNSRoot -ine 'corp.local') { throw 'Source domain must be corp.local.' }
$forest = Get-ADForest -Server $node.Name
$now = [DateTime]::UtcNow.ToString('o')
$source = $domain.ObjectGUID.ToString()
$objects = New-Object 'System.Collections.Generic.List[object]'
$truncated = $false
$allowed = @('objectSid','sAMAccountName','userPrincipalName','displayName','memberOf','groupType','userAccountControl','servicePrincipalName','gPLink','gPOptions')
Get-ADObject -Server $node.Name -LDAPFilter '(|(objectClass=user)(objectClass=group)(objectClass=organizationalUnit)(objectClass=groupPolicyContainer))' -SearchBase $domain.DistinguishedName -Properties $allowed -ResultSetSize 50001 | ForEach-Object {
if ($objects.Count -ge 50000) { $truncated = $true; return }
$item = $_
$sid = ''
if ($null -ne $item.objectSid) { $sid = [string]$item.objectSid }
$kind = [string]$item.ObjectClass
if ($kind -eq 'groupPolicyContainer') { $kind = 'gpo' }
if (@('user','group','organizationalUnit','computer','gpo') -notcontains $kind) { $kind = 'other' }
$attributes = @{}
foreach ($name in $allowed) {
if ($name -ne 'objectSid' -and $name -ne 'memberOf' -and $null -ne $item.$name) {
$attributes[$name] = @($item.$name | ForEach-Object { [string]$_ })
}
}
$objects.Add(@{
source_system_id = $source; object_guid = $item.ObjectGUID.ToString(); sid = $sid
dn = $item.DistinguishedName; object_type = $kind; attributes = $attributes
memberships = @($item.memberOf | ForEach-Object { [string]$_ }); observed_at = $now
})
}
@{
schema_version = 1; source_system_id = $source; source_domain = $domain.DNSRoot; synthetic = $false
scope = @{
description = ('Directory naming context: ' + $domain.DistinguishedName + '; forest mode: ' + $forest.ForestMode + '; domain mode: ' + $domain.DomainMode)
complete = $false
denied_or_unreachable = @('DNS/DHCP zone and lease content not collected','File data and ACLs not collected','GPO reports, SYSVOL and effective outcomes not collected','Service/application bindings and AD CS not collected','Password/profile continuity not evaluated',$(if ($truncated) { 'Object limit reached: partial inventory' } else { 'Directory ACL visibility must be verified by source owner' }))
categories = @('users','groups','ou','computers','gpo_links')
}
hosts = @(); objects = @($objects.ToArray())
} | ConvertTo-Json -Depth 16 -Compress