# Instala el conector FoxPro como tarea programada que arranca con Windows y se reinicia si cae. # Ejecutar en PowerShell como administrador, desde C:\api_project\foxpro: # powershell -ExecutionPolicy Bypass -File .\scripts\instalar-servicio.ps1 # Opcional: -Usuario 'DOMINIO\svc_conector' (cuenta con SOLO lectura sobre las carpetas de datos). param( [string]$Usuario = 'SYSTEM', [string]$Clave = '', [int]$Puerto = 8455, [string[]]$IpsPermitidas = @('192.168.222.3', '192.168.222.40') ) $ErrorActionPreference = 'Stop' $App = Split-Path -Parent $PSScriptRoot $Tarea = 'LinkedCorp-Conector-FoxPro' # node.exe portable dentro de la carpeta (no requiere instalar Node en el servidor); si no, el del sistema. $Node = Join-Path $App 'node\node.exe' if (-not (Test-Path $Node)) { $Node = (Get-Command node.exe -ErrorAction Stop).Source } if (-not (Test-Path (Join-Path $App 'config.json'))) { throw 'Falta config.json (copie config.example.json y complételo).' } $accion = New-ScheduledTaskAction -Execute $Node -Argument "`"$App\src\server.js`"" -WorkingDirectory $App $disparador = New-ScheduledTaskTrigger -AtStartup $ajustes = New-ScheduledTaskSettingsSet -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) ` -ExecutionTimeLimit ([TimeSpan]::Zero) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable if (Get-ScheduledTask -TaskName $Tarea -ErrorAction SilentlyContinue) { Stop-ScheduledTask -TaskName $Tarea -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $Tarea -Confirm:$false } if ($Usuario -eq 'SYSTEM') { Register-ScheduledTask -TaskName $Tarea -Action $accion -Trigger $disparador -Settings $ajustes -User 'SYSTEM' -RunLevel Highest | Out-Null } else { Register-ScheduledTask -TaskName $Tarea -Action $accion -Trigger $disparador -Settings $ajustes -User $Usuario -Password $Clave | Out-Null } # Firewall: el puerto solo acepta a las IPs del backend (además del filtro allowIps del propio conector). $Regla = 'LinkedCorp-Conector-FoxPro' Get-NetFirewallRule -DisplayName $Regla -ErrorAction SilentlyContinue | Remove-NetFirewallRule New-NetFirewallRule -DisplayName $Regla -Direction Inbound -Protocol TCP -LocalPort $Puerto ` -RemoteAddress $IpsPermitidas -Action Allow | Out-Null Start-ScheduledTask -TaskName $Tarea Write-Host "Conector instalado. Tarea: $Tarea Puerto: $Puerto IPs: $($IpsPermitidas -join ', ')"