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

Write-Host "Testing SSH via CMD instead of PowerShell..." -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Testing SSH via cmd.exe..." -ForegroundColor Yellow
    $cmdResult = cmd /c "echo | C:\Progra~1\OpenSSH\ssh.exe -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@192.168.0.9 hostname 2>&1"
    Write-Host "  Result: $cmdResult" -ForegroundColor White

    Write-Host ""
    Write-Host "[2] Checking if SSH is hanging on key auth..." -ForegroundColor Yellow
    $cmdResult2 = cmd /c "echo | C:\Progra~1\OpenSSH\ssh.exe -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o BatchMode=yes root@192.168.0.9 hostname 2>&1"
    Write-Host "  Result with BatchMode: $cmdResult2" -ForegroundColor White

    Write-Host ""
    Write-Host "[3] Killing any stuck SSH processes..." -ForegroundColor Yellow
    $sshProcs = Get-Process -Name ssh -ErrorAction SilentlyContinue
    if ($sshProcs) {
        $sshProcs | ForEach-Object {
            Write-Host "  Killing SSH PID: $($_.Id)" -ForegroundColor Red
            Stop-Process -Id $_.Id -Force
        }
    } else {
        Write-Host "  No SSH processes found" -ForegroundColor Green
    }
}
