Public/Objects/Get-ADComputers.ps1
|
function Get-ADComputers { [CmdletBinding()] param( [string]$ObjectType = "Computers" ) try { Write-Log "Retrieving computer accounts from cached data..." -Level Info Show-ProgressHelper -Activity "AD Inventory" -Status "Initializing computer retrieval..." # Check if cached data is available if (-not $script:AllComputers) { Write-Log "No cached computer data found." -Level Warning return $null } $computerObjects = Get-ADObjects -ObjectType $ObjectType -Objects $script:AllComputers -ProcessingScript { param($computer) try { $computerObject = [PSCustomObject]@{ Name = $computer.Name IPv4Address = $computer.IPv4Address DNSHostName = $computer.DNSHostName OperatingSystem = $computer.OperatingSystem OperatingSystemVersion = $computer.OperatingSystemVersion Enabled = $computer.Enabled LastLogonDate = $computer.LastLogonDate Created = $computer.Created Modified = $computer.Modified DistinguishedName = $computer.DistinguishedName ServicePrincipalNames = $computer.ServicePrincipalNames MemberOf = $computer.MemberOf AccessStatus = "Success" NetworkStatus = "Unknown" IsAlive = $false } Add-Member -InputObject $computerObject -MemberType ScriptMethod -Name "ToString" -Value { "Name=$($this.Name); NetworkStatus=$($this.NetworkStatus); IsAlive=$($this.IsAlive); Groups=$($this.MemberOf.Count)" } -Force $computerObject } catch { Write-Log "Error processing computer $($computer.Name): $($_.Exception.Message)" -Level Warning $computerObject = [PSCustomObject]@{ Name = $computer.Name IPv4Address = $null DNSHostName = $null OperatingSystem = $null OperatingSystemVersion = $null Enabled = $null LastLogonDate = $null Created = $null Modified = $null DistinguishedName = $computer.DistinguishedName ServicePrincipalNames = $null MemberOf = @() AccessStatus = "Access Error: $($_.Exception.Message)" NetworkStatus = "Error" IsAlive = $false } Add-Member -InputObject $computerObject -MemberType ScriptMethod -Name "ToString" -Value { "Name=$($this.Name); NetworkStatus=Error; IsAlive=$($this.IsAlive); Groups=0" } -Force $computerObject } } return $computerObjects } catch { Write-Log "Error retrieving computers: $($_.Exception.Message)" -Level Error } } |