# Check the sync script implementation
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Analyzing Sync Script ===" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    $scriptPath = "C:\Shares\test\scripts\Sync-FromNAS.ps1"

    Write-Host "[1] Script Push Implementation" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # Find the push/pscp commands
    $content = Get-Content $scriptPath
    $inPushSection = $false
    $lineNum = 0

    foreach ($line in $content) {
        $lineNum++

        # Look for PUSH section or pscp commands
        if ($line -match 'PUSH|pscp|Push files|Push File|ERROR.*push') {
            Write-Host "$lineNum : $line" -ForegroundColor White
            $inPushSection = $true
        } elseif ($inPushSection -and $line.Trim() -eq "") {
            $inPushSection = $false
        } elseif ($inPushSection) {
            Write-Host "$lineNum : $line" -ForegroundColor Gray
        }
    }

    Write-Host ""
    Write-Host "[2] Check for Failed File Patterns" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # Check if one of the failing files exists on NAS
    Write-Host "Attempting SSH to NAS to check file presence..." -ForegroundColor Yellow

    # Try to connect to NAS and check
    $nasCheck = & plink.exe -batch root@192.168.0.9 "ls -la /data/test/TS-11L/ProdSW/HVDATA/hvin.dat 2>&1" 2>&1

    if ($LASTEXITCODE -eq 0) {
        Write-Host "File EXISTS on NAS:" -ForegroundColor Green
        Write-Host $nasCheck -ForegroundColor White
    } else {
        Write-Host "File check result:" -ForegroundColor Red
        Write-Host $nasCheck -ForegroundColor Red
    }
}
