Files
sfera/scripts/windows-agent/install-sfera-windows-agent-service.ps1
2026-05-16 19:03:49 +03:00

83 lines
3.0 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[string]$ServerUrl,
[string]$ApiUrl = "",
[Parameter(Mandatory = $true)]
[string]$AgentId,
[string]$TaskName = "SferaWindowsAgent",
[string]$ServiceName = "SferaWindowsAgent",
[string]$InstallDir = "C:\ProgramData\SFERA\WindowsAgent",
[string[]]$NetworkRoot = @()
)
$ErrorActionPreference = "Stop"
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw "Run this installer in PowerShell as Administrator."
}
$sourceScript = Join-Path $PSScriptRoot "sfera-windows-agent.ps1"
if (!(Test-Path -LiteralPath $sourceScript -PathType Leaf)) {
throw "Agent script not found: $sourceScript"
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$targetScript = Join-Path $InstallDir "sfera-windows-agent.ps1"
$configPath = Join-Path $InstallDir "agent-config.json"
Copy-Item -LiteralPath $sourceScript -Destination $targetScript -Force
$config = [ordered]@{
server_url = $ServerUrl.TrimEnd("/")
api_url = $ApiUrl.TrimEnd("/")
agent_id = $AgentId
poll_seconds = 5
network_roots = @($NetworkRoot)
}
$config | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $configPath -Encoding UTF8
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($existingTask) {
Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
}
$arguments = @(
"-STA",
"-NoProfile",
"-WindowStyle", "Hidden",
"-ExecutionPolicy", "Bypass",
"-File", "`"$targetScript`"",
"-ConfigPath", "`"$configPath`""
)
$existing = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($existing) {
sc.exe stop $ServiceName | Out-Null
sc.exe delete $ServiceName | Out-Null
Start-Sleep -Seconds 2
}
$powerShellPath = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
if (!(Test-Path -LiteralPath $powerShellPath -PathType Leaf)) {
$powerShellPath = Join-Path $PSHOME "powershell.exe"
}
$action = New-ScheduledTaskAction -Execute $powerShellPath -Argument ($arguments -join " ")
$trigger = New-ScheduledTaskTrigger -AtLogOn -User "$env:USERDOMAIN\$env:USERNAME"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType Interactive -RunLevel Highest
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Settings $settings -Principal $principal -Description "SFERA bridge between local 1C/EDT/network folders and SFERA server." -Force | Out-Null
Start-ScheduledTask -TaskName $TaskName
Write-Host "Installed and started scheduled task $TaskName."
Write-Host "ServerUrl=$ServerUrl"
Write-Host "AgentId=$AgentId"
Write-Host "Config=$configPath"
if ($NetworkRoot.Count -gt 0) {
Write-Host "NetworkRoot=$($NetworkRoot -join ', ')"
}