103 lines
4.6 KiB
PowerShell
103 lines
4.6 KiB
PowerShell
param(
|
|
[string]$TargetUser = 'user'
|
|
)
|
|
|
|
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$targetProfile = "C:\Users\$TargetUser"
|
|
if (-not (Test-Path $targetProfile)) { throw "Windows user '$TargetUser' not found ($targetProfile missing)" }
|
|
|
|
$installDir = Join-Path $targetProfile 'AppData\Roaming\Telegram Desktop'
|
|
$startMenuDir = Join-Path $targetProfile 'AppData\Roaming\Microsoft\Windows\Start Menu\Programs'
|
|
$desktopDir = Join-Path $targetProfile 'Desktop'
|
|
|
|
function Get-TelegramVersion($dir) {
|
|
$exe = Join-Path $dir 'Telegram.exe'
|
|
if (Test-Path $exe) { return (Get-Item $exe).VersionInfo.ProductVersion }
|
|
return $null
|
|
}
|
|
|
|
# Already installed at target?
|
|
$existingVer = Get-TelegramVersion $installDir
|
|
if ($existingVer) {
|
|
Write-Host "[=] Already installed for '$TargetUser': Telegram $existingVer at $installDir"
|
|
exit 0
|
|
}
|
|
|
|
# Clean up any stray Admin-scoped install that we may have created earlier
|
|
$adminDir = 'C:\Users\Admin\AppData\Roaming\Telegram Desktop'
|
|
$adminUninst = Join-Path $adminDir 'unins000.exe'
|
|
if ((Test-Path $adminUninst) -and ($TargetUser -ne 'Admin')) {
|
|
Write-Host "[*] Removing stray Admin-scoped install at $adminDir"
|
|
Get-Process Telegram,Updater -ErrorAction SilentlyContinue | Stop-Process -Force
|
|
$p = Start-Process -FilePath $adminUninst -ArgumentList '/VERYSILENT','/SUPPRESSMSGBOXES','/NORESTART' -Wait -PassThru
|
|
if ($p.ExitCode -ne 0) { Write-Host "[!] Uninstall exited $($p.ExitCode), continuing anyway" }
|
|
|
|
# Remove Admin's HKCU uninstall key (best-effort)
|
|
Get-Item 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' -ErrorAction SilentlyContinue |
|
|
Where-Object { (Get-ItemProperty $_.PSPath).DisplayName -match 'Telegram' } |
|
|
ForEach-Object { Remove-Item $_.PSPath -Force -Recurse -ErrorAction SilentlyContinue }
|
|
# Remove Admin's start menu shortcut
|
|
$adminLnk = 'C:\Users\Admin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Telegram.lnk'
|
|
Remove-Item $adminLnk -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
|
|
# Download installer
|
|
$url = 'https://telegram.org/dl/desktop/win64'
|
|
$dst = Join-Path $env:TEMP 'telegram-setup.exe'
|
|
Write-Host "[*] Downloading installer -> $dst"
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing
|
|
if (-not (Test-Path $dst) -or (Get-Item $dst).Length -lt 10MB) {
|
|
throw "Downloaded file looks wrong (< 10 MB)"
|
|
}
|
|
|
|
# Install into target user's AppData. /NOICONS = no shortcuts (we'll create them in target's profile).
|
|
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
|
|
$argStr = '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /NOICONS /NOLAUNCH /DIR="' + $installDir + '"'
|
|
Write-Host "[*] Installing to $installDir"
|
|
$p = Start-Process -FilePath $dst -ArgumentList $argStr -Wait -PassThru
|
|
Remove-Item $dst -ErrorAction SilentlyContinue
|
|
if ($p.ExitCode -ne 0) { throw "Installer exited $($p.ExitCode)" }
|
|
|
|
# Verify binary appeared
|
|
$finalVer = Get-TelegramVersion $installDir
|
|
if (-not $finalVer) { throw "Telegram.exe not found at $installDir after install" }
|
|
|
|
# Give target user full control over the install tree
|
|
Write-Host "[*] Adjusting ACL on $installDir for '$TargetUser'"
|
|
& icacls $installDir /grant "${TargetUser}:(OI)(CI)F" /T /Q | Out-Null
|
|
|
|
# Create Start Menu shortcut in target user's profile
|
|
New-Item -ItemType Directory -Force -Path $startMenuDir | Out-Null
|
|
$wsh = New-Object -ComObject WScript.Shell
|
|
$lnk = $wsh.CreateShortcut((Join-Path $startMenuDir 'Telegram.lnk'))
|
|
$lnk.TargetPath = Join-Path $installDir 'Telegram.exe'
|
|
$lnk.WorkingDirectory = $installDir
|
|
$lnk.IconLocation = (Join-Path $installDir 'Telegram.exe') + ',0'
|
|
$lnk.Save()
|
|
Write-Host "[*] Start menu shortcut: $(Join-Path $startMenuDir 'Telegram.lnk')"
|
|
|
|
# Desktop shortcut
|
|
if (Test-Path $desktopDir) {
|
|
$dlnk = $wsh.CreateShortcut((Join-Path $desktopDir 'Telegram.lnk'))
|
|
$dlnk.TargetPath = Join-Path $installDir 'Telegram.exe'
|
|
$dlnk.WorkingDirectory = $installDir
|
|
$dlnk.IconLocation = (Join-Path $installDir 'Telegram.exe') + ',0'
|
|
$dlnk.Save()
|
|
Write-Host "[*] Desktop shortcut: $(Join-Path $desktopDir 'Telegram.lnk')"
|
|
}
|
|
|
|
# Fix ACL on the shortcuts too
|
|
& icacls (Join-Path $startMenuDir 'Telegram.lnk') /grant "${TargetUser}:F" /Q | Out-Null
|
|
if (Test-Path (Join-Path $desktopDir 'Telegram.lnk')) {
|
|
& icacls (Join-Path $desktopDir 'Telegram.lnk') /grant "${TargetUser}:F" /Q | Out-Null
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "[OK] Telegram $finalVer installed for '$TargetUser'"
|
|
Write-Host " Location: $installDir"
|
|
Write-Host " Data (tdata) will be created beside the exe on first launch."
|