Public/Get-ADComputers.ps1
|
function Get-ADComputers { [CmdletBinding()] param( [switch]$Export, [string]$ExportPath = $script:Config.ExportPath ) try { Write-Log "Retrieving computer accounts..." -Level Info Show-ProgressHelper -Activity "AD Inventory" -Status "Initializing computer retrieval..." $properties = @( 'Name', 'DistinguishedName', 'OperatingSystem', 'OperatingSystemVersion', 'Enabled', 'LastLogonDate', 'Created', 'Modified', 'DNSHostName' ) $allComputers = Invoke-WithRetry -ScriptBlock { Get-ADComputer -Filter * -Properties $properties -ErrorAction Stop } $computers = Get-ADObjects -ObjectType "Computers" -Objects $allComputers -ProcessingScript { param($computer) $computer | Select-Object $properties } # Generate and display statistics $stats = Get-CollectionStatistics -Data $computers -ObjectType "Computers" Write-Host "`n=== Computer Collection Statistics ===" Write-Host "Total Computers: $($stats.TotalCount)" Write-Host "`nDistribution by OU:" $stats.OUDistribution.GetEnumerator() | Sort-Object Name | ForEach-Object { Write-Host ("{0,-50} : {1,5}" -f $_.Key, $_.Value) } if ($Export) { if (-not (Test-Path $ExportPath)) { New-Item -ItemType Directory -Path $ExportPath -Force } $exportFile = Join-Path $ExportPath "Computers_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv" $computers | Export-Csv $exportFile -NoTypeInformation Write-Log "Computers exported to $exportFile" -Level Info } Show-ProgressHelper -Activity "AD Inventory" -Status "Computer retrieval complete" -Completed return $computers } catch { Write-Log "Error retrieving computers: $($_.Exception.Message)" -Level Error Show-ErrorBox "Unable to retrieve computer accounts. Check permissions." } } |