<#
.SYNOPSIS
    Generates a report of all Active Directory computers.

.DESCRIPTION
    This script queries Active Directory for all computer accounts and exports
    key properties including name, operating system, last logon, and OU location.

.PARAMETER OutputPath
    Optional. Path to export CSV report. If not specified, outputs to console.

.PARAMETER OperatingSystem
    Optional. Filter by operating system (e.g., "Windows Server*", "*Windows 10*").

.EXAMPLE
    .\Get-ADComputerReport.ps1
    Lists all computers to console.

.EXAMPLE
    .\Get-ADComputerReport.ps1 -OperatingSystem "Windows Server*" -OutputPath "C:\ClaudeTools\Logs\servers.csv"
    Exports all Windows Server computers to CSV.

.NOTES
    Author: ClaudeTools Automation
    Version: 1.0
    Requires: ActiveDirectory PowerShell module
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory=$false)]
    [string]$OutputPath,

    [Parameter(Mandatory=$false)]
    [string]$OperatingSystem = "*"
)

# Import AD module
Import-Module ActiveDirectory -ErrorAction Stop

Write-Host "Querying Active Directory computers..." -ForegroundColor Cyan

# Get computers with properties
$computers = Get-ADComputer -Filter "OperatingSystem -like '$OperatingSystem'" -Properties `
    OperatingSystem,
    OperatingSystemVersion,
    LastLogonDate,
    Created,
    Enabled,
    IPv4Address,
    Description,
    DistinguishedName |
    Select-Object `
        @{N='Name';E={$_.Name}},
        @{N='OperatingSystem';E={$_.OperatingSystem}},
        @{N='OSVersion';E={$_.OperatingSystemVersion}},
        @{N='Enabled';E={$_.Enabled}},
        @{N='IPv4Address';E={$_.IPv4Address}},
        @{N='LastLogon';E={$_.LastLogonDate}},
        @{N='Created';E={$_.Created}},
        @{N='OU';E={($_.DistinguishedName -split ',',2)[1]}},
        @{N='Description';E={$_.Description}}

$computerCount = ($computers | Measure-Object).Count
Write-Host "Found $computerCount computers." -ForegroundColor Green

if ($OutputPath) {
    $computers | Export-Csv -Path $OutputPath -NoTypeInformation
    Write-Host "Report exported to: $OutputPath" -ForegroundColor Green
} else {
    $computers | Format-Table -AutoSize
}

# Summary by OS
Write-Host "`n--- Operating System Summary ---" -ForegroundColor Yellow
$computers | Group-Object OperatingSystem | Sort-Object Count -Descending |
    Format-Table @{N='Operating System';E={$_.Name}}, Count -AutoSize

# Summary by status
$enabledCount = ($computers | Where-Object { $_.Enabled -eq $true } | Measure-Object).Count
$disabledCount = ($computers | Where-Object { $_.Enabled -eq $false } | Measure-Object).Count
Write-Host "Enabled: $enabledCount | Disabled: $disabledCount"

# Stale computers (no logon in 90 days)
$staleDate = (Get-Date).AddDays(-90)
$staleCount = ($computers | Where-Object { $_.LastLogon -lt $staleDate -or $null -eq $_.LastLogon } | Measure-Object).Count
Write-Host "Stale (no logon 90+ days): $staleCount" -ForegroundColor $(if ($staleCount -gt 0) { 'Yellow' } else { 'Green' })
