Initial LanUps implementation and native installers

This commit is contained in:
tongtong committed 2026-11-28 16:26:39 +08:00
commit 67a043c89b
66 files changed
+2870

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
@echo off
setlocal
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0Setup.ps1" %*
if errorlevel 1 pause
+57
View File
@@ -0,0 +1,57 @@
param([string]$Operator = [Security.Principal.WindowsIdentity]::GetCurrent().Name, [switch]$EnableShutdown)
$ErrorActionPreference = 'Stop'
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
if (!([Security.Principal.WindowsPrincipal]::new($identity)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
if ($Operator -match '["\r\n]') { throw 'Invalid operator account' }
$arguments = '-NoProfile -ExecutionPolicy Bypass -File "{0}" -Operator "{1}"' -f $PSCommandPath, $Operator
if ($EnableShutdown) { $arguments += ' -EnableShutdown' }
$process = Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments -Wait -PassThru -WindowStyle Hidden
exit $process.ExitCode
}
$destination = Join-Path $env:ProgramFiles 'LanUps'
$logDirectory = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'LanUps'
New-Item -ItemType Directory -Path $logDirectory -Force | Out-Null
$log = Join-Path $logDirectory 'setup.log'
Start-Transcript -Path $log -Append | Out-Null
try {
foreach ($kind in @('Agent', 'Desktop')) {
if (!(Test-Path -LiteralPath (Join-Path $PSScriptRoot "$kind\LanUps.$kind.exe") -PathType Leaf)) {
throw "Extract the entire installer archive before running Setup.cmd: missing $kind"
}
}
$service = Get-Service 'LanUps.Agent' -ErrorAction SilentlyContinue
if ($service -and $service.Status -ne 'Stopped') { Stop-Service $service }
$task = Get-ScheduledTask -TaskName 'LanUps Desktop' -ErrorAction SilentlyContinue
if ($task -and $task.State -eq 'Running') { Stop-ScheduledTask $task }
$existingDesktop = Join-Path $destination 'Desktop\LanUps.Desktop.exe'
Get-Process LanUps.Desktop -ErrorAction SilentlyContinue | Where-Object Path -EQ $existingDesktop |
ForEach-Object { Stop-Process -Id $_.Id; $_.WaitForExit() }
foreach ($kind in @('Agent', 'Desktop')) {
$target = Join-Path $destination $kind
New-Item -ItemType Directory -Path $target -Force | Out-Null
Copy-Item -Path (Join-Path $PSScriptRoot "$kind\*") -Destination $target -Recurse -Force
}
& (Join-Path $PSScriptRoot 'install.ps1') -Operator $Operator -EnableShutdown:$EnableShutdown
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut((Join-Path ([Environment]::GetFolderPath('CommonDesktopDirectory')) '局域网 UPS.lnk'))
$shortcut.TargetPath = $existingDesktop
$shortcut.WorkingDirectory = (Split-Path -Parent $existingDesktop)
$shortcut.IconLocation = "$existingDesktop,0"
$shortcut.Save()
if ($identity.Name -ieq $Operator) {
$personalPath = Join-Path ([Environment]::GetFolderPath('DesktopDirectory')) '局域网 UPS.lnk'
if (Test-Path -LiteralPath $personalPath) {
$personal = $shell.CreateShortcut($personalPath)
if ((Split-Path -Leaf $personal.TargetPath) -ieq 'LanUps.Desktop.exe') {
$personal.TargetPath = $existingDesktop
$personal.WorkingDirectory = (Split-Path -Parent $existingDesktop)
$personal.IconLocation = "$existingDesktop,0"
$personal.Save()
}
}
}
Write-Host "Installed: $destination"
} finally { Stop-Transcript | Out-Null }
+56
View File
@@ -0,0 +1,56 @@
#Requires -RunAsAdministrator
param(
[Parameter(Mandatory)][string]$Operator,
[switch]$EnableShutdown
)
$ErrorActionPreference = 'Stop'
$appRoot = Join-Path $env:ProgramFiles 'LanUps'
$agent = Join-Path $appRoot 'Agent\LanUps.Agent.exe'
$desktop = Join-Path $appRoot 'Desktop\LanUps.Desktop.exe'
foreach ($app in @($agent, $desktop)) {
if (!(Test-Path -LiteralPath $app -PathType Leaf)) { throw "Missing $app" }
}
$operatorSid = ([Security.Principal.NTAccount]::new($Operator)).Translate([Security.Principal.SecurityIdentifier]).Value
$data = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'LanUps'
$token = Join-Path $data 'agent.token'
New-Item -ItemType Directory -Path $data -Force | Out-Null
$system = [Security.Principal.SecurityIdentifier]::new('S-1-5-18')
$administrators = [Security.Principal.SecurityIdentifier]::new('S-1-5-32-544')
$operatorId = [Security.Principal.SecurityIdentifier]::new($operatorSid)
$directoryRules = [Security.AccessControl.DirectorySecurity]::new()
$directoryRules.SetAccessRuleProtection($true, $false)
foreach ($principalId in @($system, $administrators)) {
$directoryRules.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($principalId, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow'))
}
$directoryRules.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($operatorId, 'ReadAndExecute', 'Allow'))
Set-Acl -LiteralPath $data -AclObject $directoryRules
if (!(Test-Path -LiteralPath $token)) {
& $agent --provision
if ($LASTEXITCODE) { throw 'Unable to create local Agent credential' }
}
$tokenRules = [Security.AccessControl.FileSecurity]::new()
$tokenRules.SetAccessRuleProtection($true, $false)
foreach ($principalId in @($system, $administrators)) {
$tokenRules.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($principalId, 'FullControl', 'Allow'))
}
$tokenRules.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($operatorId, 'Read', 'Allow'))
Set-Acl -LiteralPath $token -AclObject $tokenRules
$command = '"' + $agent + '"'
if ($EnableShutdown) { $command += ' --enable-shutdown' }
$installed = Get-CimInstance Win32_Service -Filter "Name='LanUps.Agent'"
if ($installed) {
Stop-Service 'LanUps.Agent' -ErrorAction Stop
$changed = Invoke-CimMethod -InputObject $installed -MethodName Change -Arguments @{PathName=$command;StartMode='Automatic'}
if ($changed.ReturnValue) { throw "Cannot update Agent service: $($changed.ReturnValue)" }
} else {
New-Service -Name 'LanUps.Agent' -BinaryPathName $command -DisplayName 'LAN UPS Agent' -StartupType Automatic | Out-Null
}
Start-Service -Name 'LanUps.Agent'
$action = New-ScheduledTaskAction -Execute $desktop -Argument '--tray' -WorkingDirectory (Split-Path -Parent $desktop)
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $Operator
$principal = New-ScheduledTaskPrincipal -UserId $Operator -LogonType Interactive -RunLevel Limited
Register-ScheduledTask -TaskName 'LanUps Desktop' -Action $action -Trigger $trigger -Principal $principal -Settings (New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero)) -Force | Out-Null
Start-ScheduledTask -TaskName 'LanUps Desktop'