73 lines
2.4 KiB
PowerShell
73 lines
2.4 KiB
PowerShell
param(
|
|
[switch]$Quiet
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
function Ensure-Dir([string]$PathValue) {
|
|
if (-not (Test-Path $PathValue)) {
|
|
New-Item -ItemType Directory -Path $PathValue -Force | Out-Null
|
|
}
|
|
}
|
|
|
|
function New-Shortcut([string]$ShortcutPath, [string]$TargetPath, [string]$WorkingDirectory, [string]$Description) {
|
|
$shell = New-Object -ComObject WScript.Shell
|
|
$shortcut = $shell.CreateShortcut($ShortcutPath)
|
|
$shortcut.TargetPath = $TargetPath
|
|
$shortcut.WorkingDirectory = $WorkingDirectory
|
|
$shortcut.Description = $Description
|
|
$shortcut.IconLocation = $TargetPath
|
|
$shortcut.Save()
|
|
}
|
|
|
|
$zipPath = Join-Path $PSScriptRoot 'printServer-app.zip'
|
|
$installRoot = Join-Path $env:LOCALAPPDATA 'Programs'
|
|
$installDir = Join-Path $installRoot 'printServer'
|
|
$tempExtractDir = Join-Path $env:TEMP ("printServer-install-" + [guid]::NewGuid().ToString('N'))
|
|
$exePath = Join-Path $installDir 'printServer.exe'
|
|
|
|
if (-not (Test-Path $zipPath)) {
|
|
throw 'Installer payload archive was not found.'
|
|
}
|
|
|
|
Ensure-Dir $installRoot
|
|
Ensure-Dir $tempExtractDir
|
|
|
|
try {
|
|
$running = Get-Process -Name 'printServer' -ErrorAction SilentlyContinue
|
|
if ($running) {
|
|
$running | Stop-Process -Force
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
|
|
if (Test-Path $installDir) {
|
|
Remove-Item -Path $installDir -Recurse -Force
|
|
}
|
|
|
|
Ensure-Dir $installDir
|
|
Expand-Archive -Path $zipPath -DestinationPath $tempExtractDir -Force
|
|
Copy-Item -Path (Join-Path $tempExtractDir '*') -Destination $installDir -Recurse -Force
|
|
|
|
if (-not (Test-Path $exePath)) {
|
|
throw 'Installed executable was not found after extraction.'
|
|
}
|
|
|
|
$desktopShortcut = Join-Path ([Environment]::GetFolderPath('Desktop')) 'printServer.lnk'
|
|
$startMenuDir = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'
|
|
$startMenuShortcut = Join-Path $startMenuDir 'printServer.lnk'
|
|
|
|
Ensure-Dir $startMenuDir
|
|
New-Shortcut -ShortcutPath $desktopShortcut -TargetPath $exePath -WorkingDirectory $installDir -Description 'printServer'
|
|
New-Shortcut -ShortcutPath $startMenuShortcut -TargetPath $exePath -WorkingDirectory $installDir -Description 'printServer'
|
|
|
|
Start-Process -FilePath $exePath -WorkingDirectory $installDir
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "printServer installed to $installDir"
|
|
}
|
|
}
|
|
finally {
|
|
if (Test-Path $tempExtractDir) {
|
|
Remove-Item -Path $tempExtractDir -Recurse -Force
|
|
}
|
|
} |