58 lines
3.2 KiB
PowerShell
58 lines
3.2 KiB
PowerShell
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 }
|