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

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

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Testing ping to NAS (192.168.0.9)..." -ForegroundColor Yellow
    $ping = Test-Connection -ComputerName 192.168.0.9 -Count 2 -ErrorAction SilentlyContinue
    if ($ping) {
        Write-Host "  [OK] Ping successful - average: $($ping | Measure-Object -Property ResponseTime -Average | Select-Object -ExpandProperty Average)ms" -ForegroundColor Green
    } else {
        Write-Host "  [ERROR] Ping failed" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[2] Testing TCP connection to port 22..." -ForegroundColor Yellow
    $tcpTest = Test-NetConnection -ComputerName 192.168.0.9 -Port 22 -InformationLevel Detailed -WarningAction SilentlyContinue
    Write-Host "  TCP Connection: $($tcpTest.TcpTestSucceeded)" -ForegroundColor $(if ($tcpTest.TcpTestSucceeded) { "Green" } else { "Red" })
    Write-Host "  Ping: $($tcpTest.PingSucceeded)" -ForegroundColor White
    Write-Host "  Route: $($tcpTest.InterfaceAlias)" -ForegroundColor White

    Write-Host ""
    Write-Host "[3] Checking Windows Firewall outbound rules..." -ForegroundColor Yellow
    $firewallRule = Get-NetFirewallRule | Where-Object { $_.Direction -eq "Outbound" -and $_.Action -eq "Block" } | Select-Object -First 5
    if ($firewallRule) {
        Write-Host "  Found $($firewallRule.Count) outbound block rules (showing first 5)" -ForegroundColor Yellow
    } else {
        Write-Host "  No outbound block rules found" -ForegroundColor Green
    }

    Write-Host ""
    Write-Host "[4] Checking current user SSH directory..." -ForegroundColor Yellow
    $sshDir = "$env:USERPROFILE\.ssh"
    if (Test-Path $sshDir) {
        Write-Host "  SSH directory exists: $sshDir" -ForegroundColor Green
        $files = Get-ChildItem $sshDir -ErrorAction SilentlyContinue
        if ($files) {
            Write-Host "  Contents:" -ForegroundColor White
            $files | ForEach-Object { Write-Host "    $($_.Name)" -ForegroundColor Gray }
        }
    } else {
        Write-Host "  [WARNING] No .ssh directory found" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[5] Trying telnet-style connection test..." -ForegroundColor Yellow
    try {
        $socket = New-Object System.Net.Sockets.TcpClient
        $socket.Connect("192.168.0.9", 22)
        if ($socket.Connected) {
            Write-Host "  [OK] Raw TCP socket connection successful" -ForegroundColor Green
            $socket.Close()
        }
    } catch {
        Write-Host "  [ERROR] Raw TCP socket connection failed: $($_.Exception.Message)" -ForegroundColor Red
    }
}
