[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$ExecutablePath ) $ErrorActionPreference = 'Stop' $serviceName = 'InventeraServer' $displayName = 'Inventera Server' $description = 'Автоматически запускает веб-сервер Inventera при старте Windows.' $postgresServiceName = 'InventeraPostgreSQL18' if (-not (Test-Path -LiteralPath $ExecutablePath -PathType Leaf)) { throw "Не найден исполняемый файл службы: $ExecutablePath" } $quotedExecutablePath = '"' + $ExecutablePath + '"' $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue if ($null -eq $service) { New-Service ` -Name $serviceName ` -BinaryPathName $quotedExecutablePath ` -DisplayName $displayName ` -Description $description ` -StartupType Automatic | Out-Null } else { if ($service.Status -ne 'Stopped') { Stop-Service -Name $serviceName -Force $service.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(30)) } & sc.exe config $serviceName 'binPath=' $quotedExecutablePath 'start=' 'auto' | Out-Null if ($LASTEXITCODE -ne 0) { throw "Не удалось обновить службу $serviceName. Код ошибки: $LASTEXITCODE" } & sc.exe description $serviceName $description | Out-Null if ($LASTEXITCODE -ne 0) { throw "Не удалось обновить описание службы. Код ошибки: $LASTEXITCODE" } } & sc.exe failure $serviceName 'reset=' '86400' 'actions=' 'restart/60000/restart/60000/restart/60000' | Out-Null if ($LASTEXITCODE -ne 0) { throw "Не удалось настроить перезапуск службы. Код ошибки: $LASTEXITCODE" } & sc.exe failureflag $serviceName '1' | Out-Null if ($LASTEXITCODE -ne 0) { throw "Не удалось включить действия при ошибке службы. Код ошибки: $LASTEXITCODE" } if ($null -ne (Get-Service -Name $postgresServiceName -ErrorAction SilentlyContinue)) { & sc.exe config $serviceName 'depend=' $postgresServiceName | Out-Null if ($LASTEXITCODE -ne 0) { throw "Не удалось настроить зависимость от PostgreSQL. Код ошибки: $LASTEXITCODE" } } Start-Service -Name $serviceName (Get-Service -Name $serviceName).WaitForStatus( 'Running', [TimeSpan]::FromSeconds(30) ) Write-Host 'Служба Inventera установлена и запущена.'