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

Write-Host "Testing Direct SSH Execution from AD2..." -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Checking known_hosts..." -ForegroundColor Yellow
    $knownHosts = Get-Content "$env:USERPROFILE\.ssh\known_hosts" -ErrorAction SilentlyContinue
    if ($knownHosts -match "192.168.0.9") {
        Write-Host "  [OK] NAS entry found in known_hosts" -ForegroundColor Green
    } else {
        Write-Host "  [WARNING] NAS not in known_hosts" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[2] Testing SSH with explicit key..." -ForegroundColor Yellow
    $sshCmd = "C:\Program Files\OpenSSH\ssh.exe"
    $sshKey = "$env:USERPROFILE\.ssh\id_ed25519"

    Write-Host "  Command: $sshCmd -i $sshKey -o ConnectTimeout=5 root@192.168.0.9 hostname" -ForegroundColor Gray

    $process = Start-Process -FilePath $sshCmd -ArgumentList "-i",$sshKey,"-o","ConnectTimeout=5","-o","StrictHostKeyChecking=no","root@192.168.0.9","hostname" -NoNewWindow -Wait -PassThru -RedirectStandardOutput "$env:TEMP\ssh_test_out.txt" -RedirectStandardError "$env:TEMP\ssh_test_err.txt"

    $stdout = Get-Content "$env:TEMP\ssh_test_out.txt" -ErrorAction SilentlyContinue
    $stderr = Get-Content "$env:TEMP\ssh_test_err.txt" -ErrorAction SilentlyContinue

    Write-Host "  Exit code: $($process.ExitCode)" -ForegroundColor White
    if ($stdout) { Write-Host "  STDOUT: $stdout" -ForegroundColor Green }
    if ($stderr) { Write-Host "  STDERR: $stderr" -ForegroundColor Red }

    Write-Host ""
    Write-Host "[3] Testing with plink (PuTTY) if available..." -ForegroundColor Yellow
    $plink = Get-Command plink -ErrorAction SilentlyContinue
    if ($plink) {
        Write-Host "  Found plink at: $($plink.Source)" -ForegroundColor Green
        $plinkResult = & plink -batch root@192.168.0.9 hostname 2>&1
        Write-Host "  Result: $plinkResult" -ForegroundColor White
    } else {
        Write-Host "  Plink not found" -ForegroundColor Gray
    }

    Write-Host ""
    Write-Host "[4] Testing SCP file transfer..." -ForegroundColor Yellow
    $testFile = "$env:TEMP\test_scp_$(Get-Date -Format 'HHmmss').txt"
    "TEST" | Out-File -FilePath $testFile -Encoding ASCII

    $scpCmd = "C:\Program Files\OpenSSH\scp.exe"
    $process = Start-Process -FilePath $scpCmd -ArgumentList "-o","ConnectTimeout=5","-o","StrictHostKeyChecking=no",$testFile,"root@192.168.0.9:/tmp/" -NoNewWindow -Wait -PassThru -RedirectStandardError "$env:TEMP\scp_test_err.txt"

    $scpErr = Get-Content "$env:TEMP\scp_test_err.txt" -ErrorAction SilentlyContinue
    Write-Host "  SCP Exit code: $($process.ExitCode)" -ForegroundColor White
    if ($scpErr) { Write-Host "  SCP STDERR: $scpErr" -ForegroundColor Red }

    Remove-Item $testFile -ErrorAction SilentlyContinue
}
