Files
TTSDream/desktop/deploy/windows/install.ps1
T

57 lines
3.2 KiB
PowerShell

#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'