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' ) Show-ProgressHelper -Activity "AD Inventory" -Status "Getting computers..." $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 } if ($Export) { Show-ProgressHelper -Activity "AD Inventory" -Status "Exporting computer data..." 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." } } |