# AD2 Automation Demo
# Demonstrates efficient WinRM operations vs individual SSH commands

Write-Host "=== AD2 Automation Demo ===" -ForegroundColor Cyan
Write-Host "Using WinRM for efficient remote operations`n"

# Setup credentials (read-only service account)
$password = ConvertTo-SecureString "vG!UCAD>=#gIk}1A3=:{+DV3" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\ClaudeTools-ReadOnly", $password)

# Example 1: Get AD User Summary
Write-Host "[1] Active Directory User Summary" -ForegroundColor Yellow
try {
    $userStats = Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
        $allUsers = Get-ADUser -Filter * -Properties Enabled, LastLogonDate
        @{
            Total = $allUsers.Count
            Enabled = ($allUsers | Where-Object Enabled -eq $true).Count
            Disabled = ($allUsers | Where-Object Enabled -eq $false).Count
            RecentLogin = ($allUsers | Where-Object { $_.LastLogonDate -gt (Get-Date).AddDays(-30) }).Count
        }
    }

    Write-Host "  Total Users: $($userStats.Total)" -ForegroundColor Green
    Write-Host "  Enabled: $($userStats.Enabled)" -ForegroundColor Green
    Write-Host "  Disabled: $($userStats.Disabled)" -ForegroundColor Green
    Write-Host "  Active (30 days): $($userStats.RecentLogin)" -ForegroundColor Green
} catch {
    Write-Host "  [ERROR] $($_.Exception.Message)" -ForegroundColor Red
}

# Example 2: Get Computer Inventory
Write-Host "`n[2] Active Directory Computer Inventory" -ForegroundColor Yellow
try {
    $computerStats = Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
        $allComputers = Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate
        @{
            Total = $allComputers.Count
            Windows = ($allComputers | Where-Object { $_.OperatingSystem -like "*Windows*" }).Count
            Servers = ($allComputers | Where-Object { $_.OperatingSystem -like "*Server*" }).Count
            Active = ($allComputers | Where-Object { $_.LastLogonDate -gt (Get-Date).AddDays(-30) }).Count
        }
    }

    Write-Host "  Total Computers: $($computerStats.Total)" -ForegroundColor Green
    Write-Host "  Windows Systems: $($computerStats.Windows)" -ForegroundColor Green
    Write-Host "  Servers: $($computerStats.Servers)" -ForegroundColor Green
    Write-Host "  Active (30 days): $($computerStats.Active)" -ForegroundColor Green
} catch {
    Write-Host "  [ERROR] $($_.Exception.Message)" -ForegroundColor Red
}

# Example 3: Check Sync Status
Write-Host "`n[3] Dataforth Sync Status" -ForegroundColor Yellow
try {
    $syncStatus = Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
        $statusFile = "C:\Shares\test\_SYNC_STATUS.txt"
        if (Test-Path $statusFile) {
            Get-Content $statusFile -Tail 5
        } else {
            "Status file not found"
        }
    }

    Write-Host "  Last Sync Status:" -ForegroundColor Green
    $syncStatus | ForEach-Object { Write-Host "    $_" -ForegroundColor Gray }
} catch {
    Write-Host "  [ERROR] $($_.Exception.Message)" -ForegroundColor Red
}

# Example 4: List Recent Logs
Write-Host "`n[4] Recent Sync Logs" -ForegroundColor Yellow
try {
    $logInfo = Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
        $logFile = "C:\Shares\test\scripts\sync-from-nas.log"
        if (Test-Path $logFile) {
            $file = Get-Item $logFile
            @{
                Size = [math]::Round($file.Length / 1KB, 2)
                LastModified = $file.LastWriteTime
                LastLines = (Get-Content $logFile -Tail 3)
            }
        } else {
            @{ Error = "Log file not found" }
        }
    }

    if ($logInfo.Error) {
        Write-Host "  [WARNING] $($logInfo.Error)" -ForegroundColor Yellow
    } else {
        Write-Host "  Log Size: $($logInfo.Size) KB" -ForegroundColor Green
        Write-Host "  Last Modified: $($logInfo.LastModified)" -ForegroundColor Green
        Write-Host "  Recent Activity:" -ForegroundColor Green
        $logInfo.LastLines | ForEach-Object { Write-Host "    $_" -ForegroundColor Gray }
    }
} catch {
    Write-Host "  [ERROR] $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== Demo Complete ===" -ForegroundColor Cyan
Write-Host "All operations completed in a single WinRM session!" -ForegroundColor Green
