@echo off setlocal cd /d "%~dp0" if not exist ".env" ( echo ERROR: .env file is missing. Download the configured ERP Local Agent package from ERP. & pause & exit /b 1 ) if not exist ".venv\Scripts\python.exe" ( py -3 -m venv .venv & if errorlevel 1 ( echo ERROR: Unable to create Python virtual environment. & pause & exit /b 1 ) ) ".venv\Scripts\python.exe" -m pip install --upgrade pip if errorlevel 1 goto :failed ".venv\Scripts\python.exe" -m pip install -r requirements.txt if errorlevel 1 goto :failed set "AGENT_DIR=%~dp0" set "AGENT_PYTHONW=%~dp0.venv\Scripts\pythonw.exe" set "SUPERVISOR=%~dp0ERPAgentSupervisor.pyw" powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; try { Stop-ScheduledTask -TaskName 'AuditFirmStorageAgent' -ErrorAction SilentlyContinue } catch {}; try { Unregister-ScheduledTask -TaskName 'AuditFirmStorageAgent' -Confirm:$false -ErrorAction SilentlyContinue } catch {}; try { Stop-ScheduledTask -TaskName 'ERP Local Agent' -ErrorAction SilentlyContinue } catch {}; try { Unregister-ScheduledTask -TaskName 'ERP Local Agent' -Confirm:$false -ErrorAction SilentlyContinue } catch {}; $d=$env:AGENT_DIR.TrimEnd('\'); $p=$env:AGENT_PYTHONW; $s=$env:SUPERVISOR; $arg='"' + $s + '" --background'; $a=New-ScheduledTaskAction -Execute $p -Argument $arg -WorkingDirectory $d; $t=New-ScheduledTaskTrigger -AtStartup; $pr=New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest; $settings=New-ScheduledTaskSettingsSet -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -ExecutionTimeLimit ([TimeSpan]::Zero) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -MultipleInstances IgnoreNew; Register-ScheduledTask -TaskName 'ERP Local Agent' -Action $a -Trigger $t -Principal $pr -Settings $settings -Force | Out-Null; Start-ScheduledTask -TaskName 'ERP Local Agent'" if errorlevel 1 goto :failed echo ERP Local Agent Supervisor installed and started successfully. echo Use the desktop shortcut "ERP Local Agent Dashboard" to open the local dashboard. exit /b 0 :failed echo ERROR: ERP Local Agent installation failed. Run this file as Administrator. exit /b 1