$ErrorActionPreference = "Stop" $pass = ConvertTo-SecureString ("Paper123" + [char]33 + "@#") -AsPlainText -Force $cred = New-Object PSCredential("INTRANET\sysadmin", $pass) $uncRoot = "\\192.168.0.6\C$" try { New-PSDrive -Name AD2 -PSProvider FileSystem -Root $uncRoot -Credential $cred -ErrorAction Stop | Out-Null Write-Output "=== DRIVE MAPPED ===" $scriptPath = "AD2:\Shares\test\scripts\Sync-FromNAS.ps1" $content = Get-Content $scriptPath -Raw # Fix 1: Invoke-NASCommand - add user@host target $old1 = '$result = & $SSH -i "C:\Users\sysadmin\.ssh\id_ed25519" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new $Command 2>&1' $new1 = '$result = & $SSH -i "C:\Users\sysadmin\.ssh\id_ed25519" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${NAS_USER}@${NAS_IP}" $Command 2>&1' $content = $content.Replace($old1, $new1) # Fix 2: Copy-FromNAS - fix the SCP line (it has the if statement on same line + unquoted paths) $old2 = '$result = & $SCP -O -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" "${NAS_USER}@${NAS_IP}:$RemotePath" $LocalPath 2>&1 if ($LASTEXITCODE -ne 0) {' $new2 = @" `$result = & `$SCP -O -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" "`${NAS_USER}@`${NAS_IP}:`"`${RemotePath}`"" "`$LocalPath" 2>&1 if (`$LASTEXITCODE -ne 0) { "@ $content = $content.Replace($old2, $new2) # Fix 3: Fix the error message in Copy-FromNAS (PUSH -> PULL) $old3 = 'Write-Log " SCP PUSH ERROR (exit $LASTEXITCODE): $errorMsg"' # Only fix the FIRST occurrence (in Copy-FromNAS). Use a targeted approach. # Since we already fixed the structure, just do a simple replace of the first occurrence $idx = $content.IndexOf($old3) if ($idx -ge 0) { $new3 = 'Write-Log " SCP PULL ERROR (exit $LASTEXITCODE): $errorMsg"' $content = $content.Substring(0, $idx) + $new3 + $content.Substring($idx + $old3.Length) } # Fix 4: Copy-ToNAS - quote both paths $old4 = '$result = & $SCP -O -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" $LocalPath "${NAS_USER}@${NAS_IP}:$RemotePath" 2>&1' $new4 = '$result = & $SCP -O -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" "$LocalPath" "${NAS_USER}@${NAS_IP}:`"${RemotePath}`"" 2>&1' $content = $content.Replace($old4, $new4) # Write the fixed script Set-Content -Path $scriptPath -Value $content -NoNewline Write-Output "=== SCRIPT UPDATED ===" # Verify the fixes $verify = Get-Content $scriptPath -Raw Write-Output "=== VERIFY: SCP LINES ===" $verify -split "`n" | Where-Object { $_ -match "result = .* SCP" } | ForEach-Object { Write-Output (" " + $_.Trim()) } Write-Output "=== VERIFY: SSH LINE ===" $verify -split "`n" | Where-Object { $_ -match "result = .* SSH" } | ForEach-Object { Write-Output (" " + $_.Trim()) } Write-Output "=== VERIFY: ERROR MESSAGES ===" $verify -split "`n" | Where-Object { $_ -match "SCP (PULL|PUSH) ERROR" } | ForEach-Object { Write-Output (" " + $_.Trim()) } Remove-PSDrive -Name AD2 -ErrorAction SilentlyContinue } catch { Write-Output "ERROR: $_" Write-Output $_.ScriptStackTrace }