Files
claudetools/clients/dataforth/scripts/restore-and-fix-sync.ps1
Mike Swanson 5cbd49ce24 Reorganize repo: compartmentalize scripts by client/project
Move 150+ scripts from root and scripts/ into client/project directories:
- clients/dataforth/scripts/ (110 files: AD2, sync, SSH, DB, DOS scripts)
- clients/bg-builders/scripts/ (14 files: Lesley mgmt, Exchange, termination)
- clients/internal-infrastructure/scripts/ (10 files: GDAP, Gitea, backups)
- projects/msp-tools/scripts/ (9 files: CIPP, MSP onboarding, Datto)
- projects/gururmm-agent/scripts/ (3 files: API test, JWT, record counts)
- clients/glaztech/scripts/ (1 file: CentraStage removal)

Also reorganized:
- VPN scripts → infrastructure/vpn-configs/
- Retrieved API/JS files → api/
- Forum posts → projects/community-forum/forum-posts/
- SSH docs → clients/internal-infrastructure/docs/
- NWTOC/CTONW docs → projects/wrightstown-smarthome/docs/
- ACG website files → projects/internal/acg-website-2025/
- Dataforth docs → clients/dataforth/docs/
- schema-retrieved.sql → docs/database/

Deleted 24 tmp_*.ps1 one-off debug scripts (preserved in git history).
Root reduced from 220+ files to 62 items (docs + directories only).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-20 17:15:07 -07:00

122 lines
5.9 KiB
PowerShell

# Restore backup and properly fix the sync script
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)
Write-Host "=== Restoring and Fixing Sync Script ===" -ForegroundColor Cyan
Write-Host ""
Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
$scriptPath = "C:\Shares\test\scripts\Sync-FromNAS.ps1"
$scriptsDir = "C:\Shares\test\scripts"
Write-Host "[1] Finding most recent backup" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Gray
$backups = Get-ChildItem "$scriptsDir\Sync-FromNAS.ps1.backup-*" | Sort-Object LastWriteTime -Descending
$latestBackup = $backups | Select-Object -First 1
if ($latestBackup) {
Write-Host "[OK] Found backup: $($latestBackup.Name)" -ForegroundColor Green
Write-Host " Created: $($latestBackup.LastWriteTime)" -ForegroundColor Gray
# Restore the backup
Copy-Item -Path $latestBackup.FullName -Destination $scriptPath -Force
Write-Host "[OK] Restored backup" -ForegroundColor Green
} else {
Write-Host "[WARNING] No backup found - using current script" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "[2] Reading original script to understand structure" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Gray
$lines = Get-Content $scriptPath
# Find the PSCP/PLINK lines and the actual function implementations
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match '^\$PSCP\s*=') {
Write-Host "Line $($i+1): $($lines[$i])" -ForegroundColor Cyan
}
if ($lines[$i] -match '^\$PLINK\s*=') {
Write-Host "Line $($i+1): $($lines[$i])" -ForegroundColor Cyan
}
if ($lines[$i] -match 'PSCP.*-ssh.*-pw') {
Write-Host "Line $($i+1) [PSCP call]: ...$(($lines[$i] -replace '.*(\$PSCP.*)', '$1'))" -ForegroundColor Yellow
}
}
Write-Host ""
Write-Host "[3] Applying targeted fixes" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Gray
# Just replace the tool paths and add better error logging
# Don't try to rewrite the functions - just improve what's there
for ($i = 0; $i -lt $lines.Count; $i++) {
# Replace tool paths
if ($lines[$i] -match '^\$PSCP\s*=\s*".*pscp\.exe"') {
$lines[$i] = '$SCP = "C:\Program Files\OpenSSH\scp.exe" # Changed from PSCP to OpenSSH'
Write-Host "[UPDATED] Line $($i+1): Tool path PSCP -> SCP" -ForegroundColor Green
}
if ($lines[$i] -match '^\$PLINK\s*=\s*".*plink\.exe"') {
$lines[$i] = '$SSH = "C:\Program Files\OpenSSH\ssh.exe" # Changed from PLINK to OpenSSH'
Write-Host "[UPDATED] Line $($i+1): Tool path PLINK -> SSH" -ForegroundColor Green
}
# Replace PSCP calls with SCP calls and add verbose flag
if ($lines[$i] -match '\$result\s*=\s*&\s*\$PSCP\s+-ssh\s+-pw\s+\$NAS_PASSWORD\s+-hostkey\s+\$NAS_HOSTKEY') {
# This is a pscp call - replace with scp
if ($lines[$i] -match 'LocalPath.*RemotePath') {
# This is Copy-ToNAS (local first, then remote)
$lines[$i] = ' $result = & $SCP -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="$SCRIPTS_DIR\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes $LocalPath "${NAS_USER}@${NAS_IP}:$RemotePath" 2>&1'
Write-Host "[UPDATED] Line $($i+1): Copy-ToNAS PSCP -> SCP" -ForegroundColor Green
} else {
# This is Copy-FromNAS (remote first, then local)
$lines[$i] = ' $result = & $SCP -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="$SCRIPTS_DIR\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes "${NAS_USER}@${NAS_IP}:$RemotePath" $LocalPath 2>&1'
Write-Host "[UPDATED] Line $($i+1): Copy-FromNAS PSCP -> SCP" -ForegroundColor Green
}
# Add error logging on the next non-empty line after "return $LASTEXITCODE"
for ($j = $i + 1; $j -lt $lines.Count; $j++) {
if ($lines[$j] -match 'return \$LASTEXITCODE') {
# Insert error logging before the return
$lines[$j] = " if (`$LASTEXITCODE -ne 0) { Write-Log `" SCP ERROR (exit $LASTEXITCODE): `$result`" }`n" + $lines[$j]
Write-Host "[ADDED] Line $($j+1): Error logging" -ForegroundColor Green
break
}
}
}
# Replace PLINK calls with SSH calls
if ($lines[$i] -match '\&\s*\$PLINK\s+-batch') {
$lines[$i] = $lines[$i] -replace '\$PLINK\s+-batch', '$SSH -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="$SCRIPTS_DIR\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes'
Write-Host "[UPDATED] Line $($i+1): PLINK -> SSH" -ForegroundColor Green
}
}
Write-Host ""
Write-Host "[4] Saving updated script" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Gray
# Save the modified script
$lines | Out-File -FilePath $scriptPath -Encoding UTF8 -Force
Write-Host "[OK] Script saved" -ForegroundColor Green
Write-Host ""
Write-Host "[5] Testing script syntax" -ForegroundColor Yellow
Write-Host "=" * 80 -ForegroundColor Gray
# Test if the script has valid syntax
try {
$null = [System.Management.Automation.PSParser]::Tokenize((Get-Content $scriptPath -Raw), [ref]$null)
Write-Host "[OK] Script syntax is valid" -ForegroundColor Green
} catch {
Write-Host "[ERROR] Script has syntax errors: $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "=== Fix Complete ===" -ForegroundColor Cyan