Update 8x8_Work_Updater.ps1

This commit is contained in:
2026-08-24 15:29:13 +01:00
parent 81a69dc432
commit 9a7f1a9044
+50 -259
View File
@@ -1,8 +1,9 @@
# ==============================================================================
# Script Name: 8x8_Work_Updater.ps1
# Description: Self-contained console updater. Uses native Win32 token
# privilege elevation to launch the interactive GUI on the
# active desktop without triggering Defender or using external tools.
# Description: Downloads 8x8 with a progress bar, schedules and launches a native
# VBScript interactive countdown directly in the user session,
# waits for user confirmation or 1-hour timeout, silently installs,
# and relaunches 8x8 Work.
# ==============================================================================
# Ensure script is running elevated
@@ -15,7 +16,7 @@ Add-Type -AssemblyName System.Net.Http
$workDir = "C:\ProgramData\JCT600_8x8Update"
$flagFile = Join-Path $workDir "8x8_Update_Proceed.flag"
$guiScript = Join-Path $workDir "Show-8x8UserGui.ps1"
$vbsScript = Join-Path $workDir "Show-8x8Prompt.vbs"
$installerPath = Join-Path $workDir "8x8_Work_Installer.msi"
if (-not (Test-Path $workDir)) {
@@ -29,163 +30,9 @@ Set-Acl $workDir $acl
if (Test-Path $flagFile) { Remove-Item $flagFile -Force }
# ------------------------------------------------------------------------------
# Native Win32 Interactive Session Process Spawner (Defender-Compliant)
# ------------------------------------------------------------------------------
$PInvokeCode = @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
public class NativeDesktopLauncher
{
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, uint BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("wtsapi32.dll", SetLastError = true)]
private static extern bool WTSQueryUserToken(uint sessionId, out IntPtr phToken);
[DllImport("kernel32.dll")]
private static extern uint WTSGetActiveConsoleSessionId();
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool CreateProcessAsUser(
IntPtr hToken,
string lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation
);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
private struct LUID
{
public uint LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
private struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
public LUID Luid;
public uint Attributes;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
private static bool EnablePrivilege(string privilegeName)
{
IntPtr hToken;
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, 0x0020 | 0x0008, out hToken)) return false;
LUID luid;
if (!LookupPrivilegeValue(null, privilegeName, out luid))
{
CloseHandle(hToken);
return false;
}
TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
tp.PrivilegeCount = 1;
tp.Luid = luid;
tp.Attributes = 0x00000002; // SE_PRIVILEGE_ENABLED
bool result = AdjustTokenPrivileges(hToken, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
CloseHandle(hToken);
return result;
}
public static bool RunAsActiveUser(string commandLine)
{
EnablePrivilege("SeAssignPrimaryTokenPrivilege");
EnablePrivilege("SeIncreaseQuotaPrivilege");
uint activeSession = WTSGetActiveConsoleSessionId();
if (activeSession == 0xFFFFFFFF) return false;
IntPtr userToken = IntPtr.Zero;
if (!WTSQueryUserToken(activeSession, out userToken)) return false;
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
si.lpDesktop = @"winsta0\default";
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
bool success = CreateProcessAsUser(
userToken,
null,
commandLine,
IntPtr.Zero,
IntPtr.Zero,
false,
0x00000010, // CREATE_NEW_CONSOLE
IntPtr.Zero,
null,
ref si,
out pi
);
if (pi.hProcess != IntPtr.Zero) CloseHandle(pi.hProcess);
if (pi.hThread != IntPtr.Zero) CloseHandle(pi.hThread);
if (userToken != IntPtr.Zero) CloseHandle(userToken);
return success;
}
}
"@
Add-Type -TypeDefinition $PInvokeCode -ErrorAction SilentlyContinue
try {
# --------------------------------------------------------------------------
# Step 1: Download 8x8 Installer with Stream Progress Bar
# Step 1: Download 8x8 Setup Installer with Console Progress Bar
# --------------------------------------------------------------------------
$installerUrl = "https://work-desktop-assets.8x8.com/prod-publish/ga/work-64-msi-v8.35.2-6.msi"
Write-Host "[1/4] Downloading 8x8 Work installer..." -ForegroundColor Cyan
@@ -224,120 +71,58 @@ try {
}
# --------------------------------------------------------------------------
# Step 2: Write User-Session GUI Script (BOM-Free UTF-8)
# Step 2: Generate VBScript UI Dialog
# --------------------------------------------------------------------------
Write-Host "[2/4] Generating countdown dialog for active desktop..." -ForegroundColor Cyan
Write-Host "[2/4] Generating user dialog..." -ForegroundColor Cyan
$guiContent = @'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$vbsContent = @"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
$flagPath = "C:\ProgramData\JCT600_8x8Update\8x8_Update_Proceed.flag"
strMsg = "An automatic update for 8x8 Work has been scheduled by IT Support." & vbCrLf & vbCrLf & _
"Please save your active work and finish ongoing calls." & vbCrLf & vbCrLf & _
"Click OK to update now, or the update will proceed automatically in 60 minutes."
$form = New-Object System.Windows.Forms.Form
$form.Text = "JCT600 IT Support — 8x8 Work Update"
$form.Size = New-Object System.Drawing.Size(460, 260)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = 'FixedDialog'
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.ControlBox = $false
$form.TopMost = $true
' 64 = Information Icon, 4096 = System Modal (Always On Top)
intReturn = objShell.Popup(strMsg, 3600, "JCT600 IT Support - 8x8 Work Update", 64 + 4096)
$script:allowClose = $false
$form.Add_FormClosing({
param($sender, $e)
if (-not $script:allowClose) { $e.Cancel = $true }
})
' Create flag file when user clicks OK or when 3600 seconds expire
Set objFile = objFSO.CreateTextFile("$flagFile", True)
objFile.WriteLine("proceed")
objFile.Close
"@
$labelHeader = New-Object System.Windows.Forms.Label
$labelHeader.Text = "JCT600 — IT SUPPORT"
$labelHeader.Location = New-Object System.Drawing.Point(20, 15)
$labelHeader.Size = New-Object System.Drawing.Size(400, 25)
$labelHeader.Font = New-Object System.Drawing.Font("Segoe UI", 12, [System.Drawing.FontStyle]::Bold)
$labelHeader.ForeColor = [System.Drawing.Color]::Navy
$labelHeader.TextAlign = 'TopCenter'
$form.Controls.Add($labelHeader)
[System.IO.File]::WriteAllText($vbsScript, $vbsContent, [System.Text.Encoding]::ASCII)
$labelMsg = New-Object System.Windows.Forms.Label
$labelMsg.Text = "An automatic update for 8x8 Work has been scheduled by IT Support.`nPlease save active work or click 'Update Now' to proceed."
$labelMsg.Location = New-Object System.Drawing.Point(20, 48)
$labelMsg.Size = New-Object System.Drawing.Size(400, 40)
$labelMsg.Font = New-Object System.Drawing.Font("Segoe UI", 9)
$labelMsg.TextAlign = 'TopCenter'
$form.Controls.Add($labelMsg)
# --------------------------------------------------------------------------
# Step 3: Launch Native Dialog inside Active User Session
# --------------------------------------------------------------------------
Write-Host "[3/4] Launching prompt on logged-in user desktop..." -ForegroundColor Cyan
$labelTimer = New-Object System.Windows.Forms.Label
$labelTimer.Location = New-Object System.Drawing.Point(20, 95)
$labelTimer.Size = New-Object System.Drawing.Size(400, 40)
$labelTimer.Font = New-Object System.Drawing.Font("Segoe UI", 20, [System.Drawing.FontStyle]::Bold)
$labelTimer.ForeColor = [System.Drawing.Color]::DarkRed
$labelTimer.TextAlign = 'MiddleCenter'
$form.Controls.Add($labelTimer)
$loggedUser = (Get-CimInstance Win32_ComputerSystem).UserName
$btnUpdateNow = New-Object System.Windows.Forms.Button
$btnUpdateNow.Text = "Update Now"
$btnUpdateNow.Font = New-Object System.Drawing.Font("Segoe UI", 10, [System.Drawing.FontStyle]::Bold)
$btnUpdateNow.Size = New-Object System.Drawing.Size(150, 40)
$btnUpdateNow.Location = New-Object System.Drawing.Point(145, 155)
$btnUpdateNow.UseVisualStyleBackColor = $true
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$script:remaining = 3600 # 1 Hour
$btnUpdateNow.Add_Click({
$timer.Stop()
$script:allowClose = $true
New-Item -Path $flagPath -ItemType File -Force | Out-Null
$form.Close()
})
$form.Controls.Add($btnUpdateNow)
$timer.Add_Tick({
if ($script:remaining -gt 0) {
$timeSpan = [TimeSpan]::FromSeconds($script:remaining)
$labelTimer.Text = $timeSpan.ToString("hh\:mm\:ss")
$script:remaining--
if ([string]::IsNullOrWhiteSpace($loggedUser)) {
Write-Warning "No active interactive user detected. Proceeding with silent install."
} else {
$timer.Stop()
$script:allowClose = $true
New-Item -Path $flagPath -ItemType File -Force | Out-Null
$form.Close()
}
})
$taskName = "JCT600_8x8_UpdatePrompt"
$initialSpan = [TimeSpan]::FromSeconds($script:remaining)
$labelTimer.Text = $initialSpan.ToString("hh\:mm\:ss")
# Schedule task to run interactively under the logged-on user account
schtasks.exe /Create /TN $taskName /TR "wscript.exe `"$vbsScript`"" /SC ONCE /ST 00:00 /IT /RU "$loggedUser" /F | Out-Null
schtasks.exe /Run /TN $taskName | Out-Null
$timer.Start()
[System.Windows.Forms.Application]::Run($form)
'@
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($guiScript, $guiContent, $utf8NoBom)
# --------------------------------------------------------------------------
# Step 3: Launch Native GUI on User's Screen & Wait
# --------------------------------------------------------------------------
$psExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
$launchCmd = "`"$psExe`" -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$guiScript`""
$launched = [NativeDesktopLauncher]::RunAsActiveUser($launchCmd)
if ($launched) {
Write-Host "Dialog displayed on screen. Waiting for user response or 1-hour expiration..." -ForegroundColor Yellow
Write-Host "Prompt sent to $loggedUser. Waiting for user confirmation or 1-hour expiration..." -ForegroundColor Yellow
while (-not (Test-Path $flagFile)) {
Start-Sleep -Seconds 2
}
} else {
Write-Warning "Could not detect active console session. Proceeding with silent install."
# Cleanup scheduled task
schtasks.exe /Delete /TN $taskName /F | Out-Null
}
# --------------------------------------------------------------------------
# Step 4: Stop 8x8 Processes & Install Silently
# Step 4: Close 8x8 Processes & Install Silently
# --------------------------------------------------------------------------
Write-Host "[3/4] Stopping 8x8 processes and installing silently..." -ForegroundColor Cyan
Write-Host "[4/4] Stopping 8x8 processes and installing silently..." -ForegroundColor Cyan
$targetProcesses = Get-Process | Where-Object {
$_.ProcessName -like "*8x8*" -or $_.MainWindowTitle -like "*8x8*"
@@ -361,10 +146,8 @@ $timer.Start()
Write-Host "Installation completed successfully." -ForegroundColor Green
# --------------------------------------------------------------------------
# Step 5: Launch Updated 8x8 Work in User Session
# Step 5: Relaunch 8x8 Work
# --------------------------------------------------------------------------
Write-Host "[4/4] Launching updated 8x8 Work..." -ForegroundColor Cyan
$possiblePaths = @(
"${env:ProgramFiles}\8x8 Inc\8x8 Work\8x8 Work.exe",
"${env:ProgramFiles(x86)}\8x8 Inc\8x8 Work\8x8 Work.exe",
@@ -375,8 +158,16 @@ $timer.Start()
$exePath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($exePath) {
[NativeDesktopLauncher]::RunAsActiveUser("`"$exePath`"") | Out-Null
Write-Host "8x8 Work launched successfully in user context." -ForegroundColor Green
if (-not [string]::IsNullOrWhiteSpace($loggedUser)) {
$launchTask = "JCT600_8x8_Launch"
schtasks.exe /Create /TN $launchTask /TR "`"$exePath`"" /SC ONCE /ST 00:00 /IT /RU "$loggedUser" /F | Out-Null
schtasks.exe /Run /TN $launchTask | Out-Null
Start-Sleep -Seconds 2
schtasks.exe /Delete /TN $launchTask /F | Out-Null
Write-Host "8x8 Work launched in user session." -ForegroundColor Green
} else {
Start-Process -FilePath $exePath
}
} else {
Write-Warning "Could not locate 8x8 Work executable."
}