Public/DomainReport/Objects/Get-ADComputers.ps1
|
function Get-ADComputers { <# .SYNOPSIS Retrieves computer accounts from Active Directory. .DESCRIPTION The Get-ADComputers function retrieves computer accounts from Active Directory, applies transformation logic, and returns processed computer objects. .EXAMPLE # Retrieve all enabled computers $computers = Get-ADComputers #> [CmdletBinding()] param( # Removed [System.Management.Automation.PSCredential]$Credential ) try { Write-Log "Retrieving computer accounts from AD..." -Level Info # Define the filter (all groups) $filter = '*' # Define the properties to retrieve $properties = @( 'IPv4Address', 'DistinguishedName', 'OperatingSystem', 'OperatingSystemVersion', 'Enabled', 'LastLogonDate', 'Created', 'Modified', 'DNSHostName', 'SID', 'ServicePrincipalNames', 'MemberOf' ) # Define the processing script for each computer $processingScript = { param($computer) # Construct the custom object with desired properties $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 a ToString method for better readability Add-Member -InputObject $computerObject -MemberType ScriptMethod -Name "ToString" -Value { "Name=$($this.Name); NetworkStatus=$($this.NetworkStatus); IsAlive=$($this.IsAlive); Groups=$($this.MemberOf.Count)" } -Force $computerObject } # Ensure that centralized credentials are set if (-not $script:adminCreds) { Write-Log "Admin credentials not found. Please run Get-DomainReport first." -Level Error return $null } # Invoke the helper function using centralized credentials return Invoke-ADRetrievalWithProgress -ObjectType "Computers" ` -Filter $filter ` -Properties $properties ` -ProcessingScript $processingScript ` -ActivityName "Retrieving Computers" } catch { Write-Log "Error retrieving computers: $($_.Exception.Message)" -Level Error } } |