$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "Testing SSH from AD2 to NAS..." -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Checking SSH executable..." -ForegroundColor Yellow
    $sshPath = Get-Command ssh -ErrorAction SilentlyContinue
    if ($sshPath) {
        Write-Host "  SSH found at: $($sshPath.Source)" -ForegroundColor Green
    } else {
        Write-Host "  [ERROR] SSH not found in PATH" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[2] Testing basic SSH connection (timeout 5s)..." -ForegroundColor Yellow
    try {
        $result = & ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@192.168.0.9 "echo CONNECTED" 2>&1
        Write-Host "  Result: $result" -ForegroundColor White
        Write-Host "  Exit code: $LASTEXITCODE" -ForegroundColor White
    } catch {
        Write-Host "  [ERROR] Exception: $($_.Exception.Message)" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[3] Checking SSH key auth..." -ForegroundColor Yellow
    $sshKeyPath = "$env:USERPROFILE\.ssh\id_rsa"
    if (Test-Path $sshKeyPath) {
        Write-Host "  SSH key exists at: $sshKeyPath" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] No SSH key found at: $sshKeyPath" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[4] Testing with verbose SSH..." -ForegroundColor Yellow
    $verboseResult = & ssh -v -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@192.168.0.9 "echo OK" 2>&1 | Select-Object -Last 10
    $verboseResult | ForEach-Object { Write-Host "  $_" -ForegroundColor Gray }
}
