Files
claudetools/CTONW.BAT
Mike Swanson ba2ed379f8 feat: Add AD2 WinRM automation and modernize sync infrastructure
Comprehensive infrastructure improvements for AD2 (Domain Controller) remote
management and NAS sync system modernization.

## AD2 Remote Access Enhancements

**WinRM Configuration:**
- Enabled PowerShell Remoting (port 5985) with full logging
- Configured TrustedHosts for LAN/VPN access (172.16.*, 192.168.*, 10.*)
- Created read-only service account (ClaudeTools-ReadOnly) for safe automation
- Set up transcript logging for all remote sessions
- Deployed 6 automation scripts to C:\ClaudeTools\Scripts\ (AD user/computer
  reports, GPO status, replication health, log rotation)

**SSH Access:**
- Installed OpenSSH Server (v10.0p2)
- Generated ED25519 key for passwordless authentication
- Configured SSH key authentication for sysadmin account

**Benefits:**
- Efficient remote operations via persistent WinRM sessions (vs individual SSH commands)
- Secure read-only access for queries (no admin rights needed)
- Comprehensive audit trail of all remote operations

## Sync System Modernization (AD2 <-> NAS)

**Replaced PuTTY with OpenSSH:**
- Migrated from pscp.exe/plink.exe to native OpenSSH scp/ssh tools
- Added verbose logging (-v flag) for detailed error diagnostics
- Implemented auto host-key acceptance (StrictHostKeyChecking=accept-new)
- Enhanced error logging to capture actual SCP failure reasons

**Problem Solved:**
- Original sync errors (738 failures) had no root cause details
- PuTTY's batch mode silently failed without error messages
- New OpenSSH implementation logs full error output to sync-from-nas.log

**Scripts Created:**
- setup-openssh-sync.ps1: SSH key generation and NAS configuration
- check-openssh-client.ps1: Verify OpenSSH availability
- restore-and-fix-sync.ps1: Update Sync-FromNAS.ps1 to use OpenSSH
- investigate-sync-errors.ps1: Analyze sync failures with context
- test-winrm.ps1: WinRM connection testing (admin + service accounts)
- demo-ad2-automation.ps1: WinRM automation examples (AD stats, sync status)

## DOS Batch File Line Ending Fixes

**Problem:** All DOS batch files had Unix (LF) line endings instead of DOS (CRLF),
causing parsing errors on DOS 6.22 machines.

**Fixed:**
- Local: 13 batch files converted to CRLF
- Remote (AD2): 492 batch files scanned, 10 converted to CRLF
- Affected files: DEPLOY.BAT, NWTOC.BAT, CTONW.BAT, UPDATE.BAT, STAGE.BAT,
  CHECKUPD.BAT, REBOOT.BAT, and station-specific batch files

**Scripts Created:**
- check-dos-line-endings.ps1: Scan and detect LF vs CRLF
- convert-to-dos.ps1: Bulk conversion to DOS format
- fix-ad2-dos-files.ps1: Remote conversion via WinRM

## Credentials & Documentation Updates

**credentials.md additions:**
- Peaceful Spirit VPN configuration (L2TP/IPSec)
- AD2 WinRM/SSH access details (both admin and service accounts)
- SSH keys and known_hosts configuration
- Complete WinRM connection examples

**Files Modified:**
- credentials.md: +91 lines (VPN, AD2 automation access)
- CTONW.BAT, NWTOC.BAT, REBOOT.BAT, STAGE.BAT: Line ending fixes
- Infrastructure configs: vpn-connect.bat, vpn-disconnect.bat (CRLF)

## Test Results

**WinRM Automation (demo-ad2-automation.ps1):**
- Retrieved 178 AD users (156 enabled, 22 disabled, 40 active)
- Retrieved 67 AD computers (67 Windows, 6 servers, 53 active)
- Checked Dataforth sync status (2,249 files pushed, 738 errors logged)
- All operations completed in single remote session (efficient!)

**Sync System:**
- OpenSSH tools confirmed available on AD2
- Backup created: Sync-FromNAS.ps1.backup-20260119-140918
- Script updated with error logging and verbose output
- Next sync run will reveal actual error causes

## Technical Decisions

1. **WinRM over SSH:** More efficient for PowerShell operations, better error
   handling, native Windows integration
2. **Service Account:** Follows least-privilege principle, safer for automated
   queries, easier audit trail
3. **OpenSSH over PuTTY:** Modern, maintained, native Windows tool, better error
   reporting, supports key authentication without external tools
4. **Verbose Logging:** Critical for debugging 738 sync errors - now we'll see
   actual SCP failure reasons (permissions, paths, network issues)

## Next Steps

1. Monitor next sync run (every 15 minutes) for detailed error messages
2. Analyze SCP error output to identify root cause of 738 failures
3. Implement SSH key authentication for NAS (passwordless)
4. Consider SFTP batch mode for more reliable transfers

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-19 14:28:24 -07:00

365 lines
11 KiB
Batchfile

@ECHO OFF
REM CTONW.BAT - Computer to Network upload script
REM Uploads local changes to network share for distribution
REM
REM Usage: CTONW [target]
REM target = COMMON (share with all machines)
REM target = MACHINE (machine-specific, default)
REM
REM Examples:
REM CTONW ??? Upload to T:\%MACHINE%\ProdSW and T:\%MACHINE%\LOGS
REM CTONW MACHINE ??? Upload to T:\%MACHINE%\ProdSW and T:\%MACHINE%\LOGS
REM CTONW COMMON ??? Upload to T:\COMMON\ProdSW (requires confirmation)
REM
REM Version: 1.2 - DOS 6.22 compatible
REM Last modified: 2026-01-19 (Separated test data to LOGS folder for database import)
REM
REM Changes in v1.2:
REM - Test data (.DAT files) now upload to LOGS folder for database import
REM - Programs and config remain in ProdSW for software distribution
REM - Subdirectory mapping: 8BDATA->8BLOG, DSCDATA->DSCLOG, etc.
REM ==================================================================
REM STEP 1: Verify machine name is set
REM ==================================================================
IF NOT "%MACHINE%"=="" GOTO CHECK_DRIVE
:NO_MACHINE
ECHO.
ECHO [ERROR] MACHINE variable not set
ECHO.
ECHO Set MACHINE in AUTOEXEC.BAT:
ECHO SET MACHINE=TS-4R
ECHO.
ECHO Then reboot or run:
ECHO SET MACHINE=TS-4R
ECHO CTONW
ECHO.
PAUSE Press any key to exit...
GOTO END
REM ==================================================================
REM STEP 2: Verify T: drive is accessible
REM ==================================================================
:CHECK_DRIVE
REM Test T: drive access by switching to it
T: 2>NUL
IF ERRORLEVEL 1 GOTO NO_T_DRIVE
REM Successfully switched to T:, go back to C:
C:
REM Double-check with NUL device test
IF NOT EXIST T:\NUL GOTO NO_T_DRIVE
GOTO CHECK_TARGET
:NO_T_DRIVE
C:
ECHO.
ECHO [ERROR] T: drive not available
ECHO.
ECHO Network drive T: must be mapped to \\D2TESTNAS\test
ECHO.
ECHO Run network startup:
ECHO C:\NET\STARTNET.BAT
ECHO.
ECHO Or map manually:
ECHO NET USE T: \\D2TESTNAS\test /YES
ECHO.
PAUSE Press any key to exit...
GOTO END
REM ==================================================================
REM STEP 3: Determine upload target
REM ==================================================================
:CHECK_TARGET
REM Default target is machine-specific
SET TARGET=MACHINE
SET TARGETDIR=T:\%MACHINE%\ProdSW
SET LOGSDIR=T:\%MACHINE%\LOGS
REM Check for COMMON parameter
IF "%1"=="COMMON" SET TARGET=COMMON
IF "%1"=="common" SET TARGET=COMMON
IF "%1"=="Common" SET TARGET=COMMON
IF "%TARGET%"=="COMMON" SET TARGETDIR=T:\COMMON\ProdSW
REM ==================================================================
REM STEP 3.5: Confirm COMMON upload (NEW - v1.1)
REM ==================================================================
IF NOT "%TARGET%"=="COMMON" GOTO DISPLAY_BANNER
ECHO.
ECHO ==============================================================
ECHO [WARNING] COMMON Upload Confirmation
ECHO ==============================================================
ECHO.
ECHO You are about to upload files to COMMON location.
ECHO This will affect ALL DOS machines at Dataforth.
ECHO.
ECHO Other machines will receive these files on next NWTOC run.
ECHO.
ECHO Are you sure you want to continue? (Y/N)
ECHO.
REM Wait for user input using CHOICE (DOS 6.22 compatible)
CHOICE /C:YN /N
IF ERRORLEVEL 2 GOTO UPLOAD_CANCELLED
IF ERRORLEVEL 1 GOTO DISPLAY_BANNER
:UPLOAD_CANCELLED
ECHO.
ECHO [INFO] Upload cancelled by user
ECHO.
ECHO No files were uploaded to COMMON.
ECHO To upload to machine-specific location, run: CTONW
ECHO.
PAUSE Press any key to exit...
GOTO END
REM ==================================================================
REM STEP 4: Display upload banner
REM ==================================================================
:DISPLAY_BANNER
ECHO.
ECHO ==============================================================
ECHO Upload: %MACHINE% to Network
ECHO ==============================================================
ECHO Source: C:\BAT, C:\ATE
IF "%TARGET%"=="COMMON" ECHO Target: %TARGETDIR%
IF "%TARGET%"=="MACHINE" ECHO Targets: %TARGETDIR% (programs)
IF "%TARGET%"=="MACHINE" ECHO %LOGSDIR% (test data)
ECHO Target type: %TARGET%
ECHO ==============================================================
ECHO.
REM ==================================================================
REM STEP 5: Verify source directories exist
REM ==================================================================
IF NOT EXIST C:\BAT\NUL GOTO NO_BAT_DIR
GOTO CHECK_TARGET_DIR
:NO_BAT_DIR
ECHO [ERROR] C:\BAT directory not found
ECHO.
ECHO No files to upload.
ECHO.
PAUSE Press any key to exit...
GOTO END
REM ==================================================================
REM STEP 6: Create target directories if needed
REM ==================================================================
:CHECK_TARGET_DIR
REM Create machine directory if uploading to machine-specific location
IF "%TARGET%"=="MACHINE" IF NOT EXIST T:\%MACHINE%\NUL MD T:\%MACHINE%
REM Create ProdSW directory
IF NOT EXIST %TARGETDIR%\NUL MD %TARGETDIR%
REM Verify ProdSW directory was created
IF NOT EXIST %TARGETDIR%\NUL GOTO TARGET_DIR_ERROR
ECHO [OK] Target directory ready: %TARGETDIR%
REM Create LOGS directory for machine-specific uploads
IF "%TARGET%"=="MACHINE" IF NOT EXIST %LOGSDIR%\NUL MD %LOGSDIR%
IF "%TARGET%"=="MACHINE" IF NOT EXIST %LOGSDIR%\NUL GOTO LOGS_DIR_ERROR
IF "%TARGET%"=="MACHINE" ECHO [OK] Logs directory ready: %LOGSDIR%
ECHO.
REM ==================================================================
REM STEP 7: Upload batch files
REM ==================================================================
ECHO [1/3] Uploading batch files from C:\BAT...
REM Backup existing files on network before overwriting
ECHO Creating backups on network (.BAK files)...
FOR %%F IN (%TARGETDIR%\*.BAT) DO COPY %%F %%~dpnF.BAK >NUL 2>NUL
REM Copy batch files to network
ECHO Copying files to %TARGETDIR%...
XCOPY C:\BAT\*.BAT %TARGETDIR%\ /Y /Q
IF ERRORLEVEL 4 GOTO UPLOAD_ERROR_INIT
IF ERRORLEVEL 2 GOTO UPLOAD_ERROR_USER
IF ERRORLEVEL 1 ECHO [WARNING] No batch files found in C:\BAT
IF NOT ERRORLEVEL 1 ECHO [OK] Batch files uploaded
ECHO.
REM ==================================================================
REM STEP 8: Upload programs and config (machine-specific only)
REM CHANGED in v1.2: Now excludes DAT files (they go to LOGS)
REM ==================================================================
IF "%TARGET%"=="COMMON" GOTO SKIP_PROGRAMS
ECHO [2/3] Uploading programs and config from C:\ATE...
REM Check if ATE directory exists
IF NOT EXIST C:\ATE\NUL GOTO NO_ATE_DIR
REM Copy programs (.EXE, .BAT, .CFG, etc.) - exclude DAT files
ECHO Copying programs to %TARGETDIR%...
XCOPY C:\ATE\*.EXE %TARGETDIR%\ /S /Y /Q >NUL 2>NUL
XCOPY C:\ATE\*.BAT %TARGETDIR%\ /S /Y /Q >NUL 2>NUL
XCOPY C:\ATE\*.CFG %TARGETDIR%\ /S /Y /Q >NUL 2>NUL
XCOPY C:\ATE\*.TXT %TARGETDIR%\ /S /Y /Q >NUL 2>NUL
ECHO [OK] Programs uploaded to ProdSW
ECHO.
GOTO UPLOAD_TEST_DATA
:NO_ATE_DIR
ECHO [INFO] C:\ATE directory not found
ECHO Only batch files were uploaded
GOTO SKIP_TEST_DATA
:SKIP_PROGRAMS
ECHO [2/3] Skipping programs/data (COMMON target only gets batch files)
ECHO.
GOTO SKIP_TEST_DATA
REM ==================================================================
REM STEP 9: Upload test data to LOGS (NEW in v1.2)
REM ==================================================================
:UPLOAD_TEST_DATA
ECHO [3/3] Uploading test data to LOGS...
REM Create log subdirectories
IF NOT EXIST %LOGSDIR%\8BLOG\NUL MD %LOGSDIR%\8BLOG
IF NOT EXIST %LOGSDIR%\DSCLOG\NUL MD %LOGSDIR%\DSCLOG
IF NOT EXIST %LOGSDIR%\HVLOG\NUL MD %LOGSDIR%\HVLOG
IF NOT EXIST %LOGSDIR%\PWRLOG\NUL MD %LOGSDIR%\PWRLOG
IF NOT EXIST %LOGSDIR%\RMSLOG\NUL MD %LOGSDIR%\RMSLOG
IF NOT EXIST %LOGSDIR%\7BLOG\NUL MD %LOGSDIR%\7BLOG
REM Upload test data files to appropriate log folders
ECHO Uploading test data files...
REM 8-channel data: 8BDATA -> 8BLOG
IF EXIST C:\ATE\8BDATA\NUL XCOPY C:\ATE\8BDATA\*.DAT %LOGSDIR%\8BLOG\ /Y /Q >NUL 2>NUL
REM DSC data: DSCDATA -> DSCLOG
IF EXIST C:\ATE\DSCDATA\NUL XCOPY C:\ATE\DSCDATA\*.DAT %LOGSDIR%\DSCLOG\ /Y /Q >NUL 2>NUL
REM HV data: HVDATA -> HVLOG
IF EXIST C:\ATE\HVDATA\NUL XCOPY C:\ATE\HVDATA\*.DAT %LOGSDIR%\HVLOG\ /Y /Q >NUL 2>NUL
REM Power data: PWRDATA -> PWRLOG
IF EXIST C:\ATE\PWRDATA\NUL XCOPY C:\ATE\PWRDATA\*.DAT %LOGSDIR%\PWRLOG\ /Y /Q >NUL 2>NUL
REM RMS data: RMSDATA -> RMSLOG
IF EXIST C:\ATE\RMSDATA\NUL XCOPY C:\ATE\RMSDATA\*.DAT %LOGSDIR%\RMSLOG\ /Y /Q >NUL 2>NUL
REM 7-channel data: 7BDATA -> 7BLOG
IF EXIST C:\ATE\7BDATA\NUL XCOPY C:\ATE\7BDATA\*.DAT %LOGSDIR%\7BLOG\ /Y /Q >NUL 2>NUL
ECHO [OK] Test data uploaded to LOGS (for database import)
GOTO UPLOAD_COMPLETE
:SKIP_TEST_DATA
REM No test data upload for COMMON target
GOTO UPLOAD_COMPLETE
REM ==================================================================
REM STEP 10: Upload complete
REM ==================================================================
:UPLOAD_COMPLETE
ECHO ==============================================================
ECHO Upload Complete
ECHO ==============================================================
ECHO.
ECHO Files uploaded to:
ECHO %TARGETDIR% (software/config)
IF "%TARGET%"=="MACHINE" ECHO %LOGSDIR% (test data for database import)
ECHO.
IF "%TARGET%"=="COMMON" ECHO [WARNING] Files uploaded to COMMON - will affect ALL machines
IF "%TARGET%"=="COMMON" ECHO Other machines will receive these files on next NWTOC
ECHO.
ECHO Backup files (.BAK) created on network
ECHO.
IF "%TARGET%"=="MACHINE" ECHO To share these files with all machines, run:
IF "%TARGET%"=="MACHINE" ECHO CTONW COMMON
ECHO.
GOTO END
REM ==================================================================
REM ERROR HANDLERS
REM ==================================================================
:TARGET_DIR_ERROR
ECHO.
ECHO [ERROR] Could not create target directory
ECHO Target: %TARGETDIR%
ECHO.
ECHO Check:
ECHO - T: drive is writable
ECHO - Sufficient disk space on T:
ECHO - Network connection is stable
ECHO - Permissions to create directories
ECHO.
PAUSE Press any key to exit...
GOTO END
:LOGS_DIR_ERROR
ECHO.
ECHO [ERROR] Could not create LOGS directory
ECHO Target: %LOGSDIR%
ECHO.
ECHO Check:
ECHO - T: drive is writable
ECHO - Sufficient disk space on T:
ECHO - Network connection is stable
ECHO - Permissions to create directories
ECHO.
PAUSE Press any key to exit...
GOTO END
:UPLOAD_ERROR_INIT
ECHO.
ECHO [ERROR] Upload initialization failed
ECHO.
ECHO Possible causes:
ECHO - Insufficient memory
ECHO - Invalid path
ECHO - Target drive not accessible
ECHO - Network connection lost
ECHO.
PAUSE Press any key to exit...
GOTO END
:UPLOAD_ERROR_USER
ECHO.
ECHO [ERROR] Upload terminated by user (Ctrl+C)
ECHO.
ECHO Upload may be incomplete!
ECHO Run CTONW again to complete upload.
ECHO.
PAUSE Press any key to exit...
GOTO END
REM ==================================================================
REM CLEANUP AND EXIT
REM ==================================================================
:END
REM Clean up environment variables
SET TARGET=
SET TARGETDIR=
SET LOGSDIR=