# Script di installazione del servizio Windows per Data Coupler # Eseguire come Amministratore param( [string]$ServicePath = "C:\Services\DataCoupler\Data_Coupler.exe", [string]$ServiceName = "DataCouplerService", [string]$DisplayName = "Data Coupler Service", [string]$Description = "Servizio per l'integrazione e trasferimento dati multi-platform" ) Write-Host "Installazione Data Coupler Windows Service..." -ForegroundColor Green # Verifica che il file eseguibile esista if (-not (Test-Path $ServicePath)) { Write-Error "File eseguibile non trovato: $ServicePath" Write-Host "Assicurati di aver pubblicato l'applicazione nella directory corretta" -ForegroundColor Yellow exit 1 } try { # Ferma il servizio se esiste $existingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if ($existingService) { Write-Host "Fermando il servizio esistente..." -ForegroundColor Yellow Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue Write-Host "Rimuovendo il servizio esistente..." -ForegroundColor Yellow sc.exe delete $ServiceName Start-Sleep -Seconds 2 } # Crea la cartella per i log se non esiste $logPath = Split-Path $ServicePath $logDir = Join-Path $logPath "logs" if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force Write-Host "Creata directory log: $logDir" -ForegroundColor Green } # Crea la directory per il database se non esiste $dbPath = "C:\ProgramData\Data_Coupler" if (-not (Test-Path $dbPath)) { New-Item -ItemType Directory -Path $dbPath -Force Write-Host "Creata directory database: $dbPath" -ForegroundColor Green # Imposta permessi per la directory del database $acl = Get-Acl $dbPath $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("NETWORK SERVICE", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") $acl.SetAccessRule($accessRule) Set-Acl $dbPath $acl Write-Host "Impostati permessi per NETWORK SERVICE su $dbPath" -ForegroundColor Green } # Installa il servizio Write-Host "Installando il servizio Windows..." -ForegroundColor Green New-Service -Name $ServiceName ` -BinaryPathName $ServicePath ` -DisplayName $DisplayName ` -Description $Description ` -StartupType Automatic ` -Credential (Get-Credential -Message "Inserisci le credenziali per il servizio (usa NETWORK SERVICE o un account dedicato)") Write-Host "Servizio installato con successo!" -ForegroundColor Green # Configura il servizio per il riavvio automatico in caso di errore Write-Host "Configurando riavvio automatico..." -ForegroundColor Yellow sc.exe failure $ServiceName reset= 60 actions= restart/5000/restart/10000/restart/30000 # Avvia il servizio Write-Host "Avviando il servizio..." -ForegroundColor Green Start-Service -Name $ServiceName # Verifica lo stato $service = Get-Service -Name $ServiceName Write-Host "Stato servizio: $($service.Status)" -ForegroundColor $(if($service.Status -eq 'Running') { 'Green' } else { 'Red' }) if ($service.Status -eq 'Running') { Write-Host "`nServizio installato e avviato con successo!" -ForegroundColor Green Write-Host "URL applicazione: http://localhost:7550" -ForegroundColor Cyan Write-Host "Per monitorare il servizio usa: Get-Service -Name $ServiceName" -ForegroundColor Yellow } else { Write-Warning "Il servizio è installato ma non è in esecuzione. Controlla i log per eventuali errori." Write-Host "Per controllare i log del servizio: Get-EventLog -LogName Application -Source 'DataCouplerService' -Newest 10" -ForegroundColor Yellow } } catch { Write-Error "Errore durante l'installazione del servizio: $($_.Exception.Message)" exit 1 }