Update 8x8_Work_Updater.ps1

This commit is contained in:
2026-07-29 16:49:36 +01:00
parent 5004bb6566
commit abfbebc520
@@ -1,248 +1,248 @@
# ============================================================================== # ==============================================================================
# Script Name: Update-8x8Work-DualSession.ps1 # Script Name: Update-8x8Work-DualSession.ps1
# Description: Downloads 8x8, launches a zero-console interactive JCT600 GUI # Description: Downloads 8x8, launches a zero-console interactive JCT600 GUI
# in the active user's session via VBScript wrapper (BOM fixed), # in the active user's session via VBScript wrapper (BOM fixed),
# waits for completion/override (1-hour countdown), silently installs, # waits for completion/override (1-hour countdown), silently installs,
# relaunches 8x8 Work, and guarantees scheduled task cleanup. # relaunches 8x8 Work, and guarantees scheduled task cleanup.
# ============================================================================== # ==============================================================================
# Ensure script is running elevated # Ensure script is running elevated
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "This script must be executed with Administrator privileges." Write-Error "This script must be executed with Administrator privileges."
exit 1 exit 1
} }
# Use C:\ProgramData\ so standard users have full read/execute access # Use C:\ProgramData\ so standard users have full read/execute access
$workDir = "C:\ProgramData\JCT600_8x8Update" $workDir = "C:\ProgramData\JCT600_8x8Update"
$flagFile = Join-Path $workDir "8x8_Update_Proceed.flag" $flagFile = Join-Path $workDir "8x8_Update_Proceed.flag"
$guiScript = Join-Path $workDir "Show-8x8UserGui.ps1" $guiScript = Join-Path $workDir "Show-8x8UserGui.ps1"
$vbsScript = Join-Path $workDir "LaunchGuiHidden.vbs" $vbsScript = Join-Path $workDir "LaunchGuiHidden.vbs"
$taskName = "JCT600_8x8_Update_Notification" $taskName = "JCT600_8x8_Update_Notification"
if (-not (Test-Path $workDir)) { if (-not (Test-Path $workDir)) {
New-Item -Path $workDir -ItemType Directory -Force | Out-Null New-Item -Path $workDir -ItemType Directory -Force | Out-Null
} }
# Clean stale flag file # Clean stale flag file
if (Test-Path $flagFile) { Remove-Item $flagFile -Force } if (Test-Path $flagFile) { Remove-Item $flagFile -Force }
try { try {
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Step 1: Download 8x8 Setup Installer # Step 1: Download 8x8 Setup Installer
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
$installerUrl = "https://work-desktop-assets.8x8.com/prod-publish/ga/work-64-msi-v8.35.2-6.msi" $installerUrl = "https://work-desktop-assets.8x8.com/prod-publish/ga/work-64-msi-v8.35.2-6.msi"
$installerPath = Join-Path $workDir "8x8_Work_Installer.msi" $installerPath = Join-Path $workDir "8x8_Work_Installer.msi"
Write-Host "[1/5] Downloading 8x8 Work installer..." -ForegroundColor Cyan Write-Host "[1/5] Downloading 8x8 Work installer..." -ForegroundColor Cyan
try { try {
Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath -UseBasicParsing Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath -UseBasicParsing
Write-Host "Download complete: $installerPath" -ForegroundColor Green Write-Host "Download complete: $installerPath" -ForegroundColor Green
} catch { } catch {
Write-Error "Failed to download installer from $installerUrl. Error: $_" Write-Error "Failed to download installer from $installerUrl. Error: $_"
exit 1 exit 1
} }
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Step 2: Write User GUI & Zero-Console VBS Launcher (BOM-Free ASCII) # Step 2: Write User GUI & Zero-Console VBS Launcher (BOM-Free ASCII)
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
Write-Host "[2/5] Generating user-session GUI and hidden launcher..." -ForegroundColor Cyan Write-Host "[2/5] Generating user-session GUI and hidden launcher..." -ForegroundColor Cyan
$guiContent = @' $guiContent = @'
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName System.Drawing
$signature = @" $signature = @"
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
"@ "@
$Win32SetWindowPos = Add-Type -memberDefinition $signature -Name "Win32SetWindowPos" -namespace Win32Functions -passThru $Win32SetWindowPos = Add-Type -memberDefinition $signature -Name "Win32SetWindowPos" -namespace Win32Functions -passThru
$flagPath = "C:\ProgramData\JCT600_8x8Update\8x8_Update_Proceed.flag" $flagPath = "C:\ProgramData\JCT600_8x8Update\8x8_Update_Proceed.flag"
$form = New-Object System.Windows.Forms.Form $form = New-Object System.Windows.Forms.Form
$form.Text = "JCT600 IT Support — 8x8 Work Update" $form.Text = "JCT600 IT Support — 8x8 Work Update"
$form.Size = New-Object System.Drawing.Size(440, 250) $form.Size = New-Object System.Drawing.Size(440, 250)
$form.StartPosition = 'CenterScreen' $form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = 'FixedDialog' $form.FormBorderStyle = 'FixedDialog'
$form.MaximizeBox = $false $form.MaximizeBox = $false
$form.MinimizeBox = $false $form.MinimizeBox = $false
$form.ControlBox = $false $form.ControlBox = $false
$form.TopMost = $true $form.TopMost = $true
$script:allowClose = $false $script:allowClose = $false
$form.Add_FormClosing({ $form.Add_FormClosing({
param($sender, $e) param($sender, $e)
if (-not $script:allowClose) { $e.Cancel = $true } if (-not $script:allowClose) { $e.Cancel = $true }
}) })
$labelHeader = New-Object System.Windows.Forms.Label $labelHeader = New-Object System.Windows.Forms.Label
$labelHeader.Text = "JCT600 — IT SUPPORT" $labelHeader.Text = "JCT600 — IT SUPPORT"
$labelHeader.Location = New-Object System.Drawing.Point(20, 15) $labelHeader.Location = New-Object System.Drawing.Point(20, 15)
$labelHeader.Size = New-Object System.Drawing.Size(380, 25) $labelHeader.Size = New-Object System.Drawing.Size(380, 25)
$labelHeader.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold) $labelHeader.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold)
$labelHeader.ForeColor = [System.Drawing.Color]::Navy $labelHeader.ForeColor = [System.Drawing.Color]::Navy
$labelHeader.TextAlign = 'TopCenter' $labelHeader.TextAlign = 'TopCenter'
$form.Controls.Add($labelHeader) $form.Controls.Add($labelHeader)
$labelMsg = New-Object System.Windows.Forms.Label $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 begin." $labelMsg.Text = "An automatic update for 8x8 Work has been scheduled by IT Support.`nPlease save active work or click 'Update Now' to begin."
$labelMsg.Location = New-Object System.Drawing.Point(20, 45) $labelMsg.Location = New-Object System.Drawing.Point(20, 45)
$labelMsg.Size = New-Object System.Drawing.Size(380, 45) $labelMsg.Size = New-Object System.Drawing.Size(380, 45)
$labelMsg.Font = New-Object System.Drawing.Font("Segoe UI", 9) $labelMsg.Font = New-Object System.Drawing.Font("Segoe UI", 9)
$labelMsg.TextAlign = 'TopCenter' $labelMsg.TextAlign = 'TopCenter'
$form.Controls.Add($labelMsg) $form.Controls.Add($labelMsg)
$labelTimer = New-Object System.Windows.Forms.Label $labelTimer = New-Object System.Windows.Forms.Label
$labelTimer.Location = New-Object System.Drawing.Point(20, 95) $labelTimer.Location = New-Object System.Drawing.Point(20, 95)
$labelTimer.Size = New-Object System.Drawing.Size(380, 40) $labelTimer.Size = New-Object System.Drawing.Size(380, 40)
$labelTimer.Font = New-Object System.Drawing.Font("Segoe UI", 20, [System.Drawing.FontStyle]::Bold) $labelTimer.Font = New-Object System.Drawing.Font("Segoe UI", 20, [System.Drawing.FontStyle]::Bold)
$labelTimer.ForeColor = [System.Drawing.Color]::DarkRed $labelTimer.ForeColor = [System.Drawing.Color]::DarkRed
$labelTimer.TextAlign = 'MiddleCenter' $labelTimer.TextAlign = 'MiddleCenter'
$form.Controls.Add($labelTimer) $form.Controls.Add($labelTimer)
$btnUpdateNow = New-Object System.Windows.Forms.Button $btnUpdateNow = New-Object System.Windows.Forms.Button
$btnUpdateNow.Text = "Update Now" $btnUpdateNow.Text = "Update Now"
$btnUpdateNow.Font = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Bold) $btnUpdateNow.Font = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Bold)
$btnUpdateNow.Size = New-Object System.Drawing.Size(140, 35) $btnUpdateNow.Size = New-Object System.Drawing.Size(140, 35)
$btnUpdateNow.Location = New-Object System.Drawing.Point(140, 150) $btnUpdateNow.Location = New-Object System.Drawing.Point(140, 150)
$btnUpdateNow.UseVisualStyleBackColor = $true $btnUpdateNow.UseVisualStyleBackColor = $true
$btnUpdateNow.Add_Click({ $btnUpdateNow.Add_Click({
$timer.Stop() $timer.Stop()
$script:allowClose = $true $script:allowClose = $true
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
$form.Close() $form.Close()
}) })
$form.Controls.Add($btnUpdateNow) $form.Controls.Add($btnUpdateNow)
$form.Add_Shown({ $form.Add_Shown({
[Win32Functions.Win32SetWindowPos]::SetWindowPos($form.Handle, [IntPtr] -1, 0, 0, 0, 0, 0x0003) | Out-Null [Win32Functions.Win32SetWindowPos]::SetWindowPos($form.Handle, [IntPtr] -1, 0, 0, 0, 0, 0x0003) | Out-Null
$form.Activate() $form.Activate()
}) })
$timer = New-Object System.Windows.Forms.Timer $timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 $timer.Interval = 1000
$script:remaining = 3600 # 1 Hour $script:remaining = 3600 # 1 Hour
$timer.Add_Tick({ $timer.Add_Tick({
if ($script:remaining -gt 0) { if ($script:remaining -gt 0) {
$timeSpan = [TimeSpan]::FromSeconds($script:remaining) $timeSpan = [TimeSpan]::FromSeconds($script:remaining)
$labelTimer.Text = $timeSpan.ToString("hh\:mm\:ss") $labelTimer.Text = $timeSpan.ToString("hh\:mm\:ss")
$script:remaining-- $script:remaining--
[Win32Functions.Win32SetWindowPos]::SetWindowPos($form.Handle, [IntPtr] -1, 0, 0, 0, 0, 0x0003) | Out-Null [Win32Functions.Win32SetWindowPos]::SetWindowPos($form.Handle, [IntPtr] -1, 0, 0, 0, 0, 0x0003) | Out-Null
} else { } else {
$timer.Stop() $timer.Stop()
$script:allowClose = $true $script:allowClose = $true
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
$form.Close() $form.Close()
} }
}) })
$initialSpan = [TimeSpan]::FromSeconds($script:remaining) $initialSpan = [TimeSpan]::FromSeconds($script:remaining)
$labelTimer.Text = $initialSpan.ToString("hh\:mm\:ss") $labelTimer.Text = $initialSpan.ToString("hh\:mm\:ss")
$timer.Start() $timer.Start()
$form.ShowDialog() | Out-Null $form.ShowDialog() | Out-Null
$timer.Dispose() $timer.Dispose()
$form.Dispose() $form.Dispose()
'@ '@
# Write GUI script cleanly # Write GUI script cleanly
[System.IO.File]::WriteAllText($guiScript, $guiContent, [System.Text.Encoding]::UTF8) [System.IO.File]::WriteAllText($guiScript, $guiContent, [System.Text.Encoding]::UTF8)
# VBScript Wrapper to suppress the console (Written as ASCII to prevent BOM errors) # VBScript Wrapper to suppress the console (Written as ASCII to prevent BOM errors)
$vbsContent = @" $vbsContent = @"
Set WshShell = CreateObject("WScript.Shell") Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File ""$guiScript""", 0, False WshShell.Run "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File ""$guiScript""", 0, False
"@ "@
[System.IO.File]::WriteAllText($vbsScript, $vbsContent, [System.Text.Encoding]::ASCII) [System.IO.File]::WriteAllText($vbsScript, $vbsContent, [System.Text.Encoding]::ASCII)
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Step 3: Trigger Interactive GUI via VBS Wrapper in User's Session # Step 3: Trigger Interactive GUI via VBS Wrapper in User's Session
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
Write-Host "[3/5] Launching dialog in active user's desktop session..." -ForegroundColor Cyan Write-Host "[3/5] Launching dialog in active user's desktop session..." -ForegroundColor Cyan
$loggedUser = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName $loggedUser = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName
if ([string]::IsNullOrEmpty($loggedUser)) { if ([string]::IsNullOrEmpty($loggedUser)) {
$explorerProc = Get-Process -Name explorer -ErrorAction SilentlyContinue | Select-Object -First 1 $explorerProc = Get-Process -Name explorer -ErrorAction SilentlyContinue | Select-Object -First 1
if ($explorerProc) { if ($explorerProc) {
$loggedUser = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $($explorerProc.Id)" | Invoke-CimMethod -MethodName GetOwner).User $loggedUser = (Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $($explorerProc.Id)" | Invoke-CimMethod -MethodName GetOwner).User
} }
} }
if ([string]::IsNullOrEmpty($loggedUser)) { if ([string]::IsNullOrEmpty($loggedUser)) {
Write-Warning "No active interactive user detected. Proceeding directly to silent update." Write-Warning "No active interactive user detected. Proceeding directly to silent update."
} else { } else {
Write-Host "Detected active user: $loggedUser" -ForegroundColor Green Write-Host "Detected active user: $loggedUser" -ForegroundColor Green
$action = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$vbsScript`"" $action = New-ScheduledTaskAction -Execute "wscript.exe" -Argument "`"$vbsScript`""
$principal = New-ScheduledTaskPrincipal -UserId $loggedUser -LogonType Interactive -RunLevel Limited $principal = New-ScheduledTaskPrincipal -UserId $loggedUser -LogonType Interactive -RunLevel Limited
Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -Force | Out-Null Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -Force | Out-Null
Start-ScheduledTask -TaskName $taskName Start-ScheduledTask -TaskName $taskName
Write-Host "Waiting for user dialog response or timer expiration (1 hour max)..." -ForegroundColor Yellow Write-Host "Waiting for user dialog response or timer expiration (1 hour max)..." -ForegroundColor Yellow
while (-not (Test-Path $flagFile)) { while (-not (Test-Path $flagFile)) {
Start-Sleep -Seconds 2 Start-Sleep -Seconds 2
} }
} }
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Step 4: Close Running 8x8 Processes & Install Silently # Step 4: Close Running 8x8 Processes & Install Silently
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
Write-Host "[4/5] Stopping 8x8 processes and installing silently..." -ForegroundColor Cyan Write-Host "[4/5] Stopping 8x8 processes and installing silently..." -ForegroundColor Cyan
$targetProcesses = Get-Process | Where-Object { $targetProcesses = Get-Process | Where-Object {
$_.ProcessName -like "*8x8*" -or $_.MainWindowTitle -like "*8x8*" $_.ProcessName -like "*8x8*" -or $_.MainWindowTitle -like "*8x8*"
} }
if ($targetProcesses) { if ($targetProcesses) {
foreach ($proc in $targetProcesses) { foreach ($proc in $targetProcesses) {
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
} }
Start-Sleep -Seconds 3 Start-Sleep -Seconds 3
} }
$msiArgs = "/i `"$installerPath`" /quiet /norestart" $msiArgs = "/i `"$installerPath`" /quiet /norestart"
$installJob = Start-Process msiexec.exe -ArgumentList $msiArgs -Wait -PassThru $installJob = Start-Process msiexec.exe -ArgumentList $msiArgs -Wait -PassThru
if ($installJob.ExitCode -ne 0 -and $installJob.ExitCode -ne 3010) { if ($installJob.ExitCode -ne 0 -and $installJob.ExitCode -ne 3010) {
Write-Error "Installation failed with exit code $($installJob.ExitCode)." Write-Error "Installation failed with exit code $($installJob.ExitCode)."
exit $installJob.ExitCode exit $installJob.ExitCode
} }
Write-Host "Installation completed successfully." -ForegroundColor Green Write-Host "Installation completed successfully." -ForegroundColor Green
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Step 5: Launch 8x8 Work # Step 5: Launch 8x8 Work
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
Write-Host "[5/5] Launching 8x8 Work..." -ForegroundColor Cyan Write-Host "[5/5] Launching 8x8 Work..." -ForegroundColor Cyan
$possiblePaths = @( $possiblePaths = @(
"${env:ProgramFiles}\8x8 Inc\8x8 Work\8x8 Work.exe", "${env:ProgramFiles}\8x8 Inc\8x8 Work\8x8 Work.exe",
"${env:ProgramFiles(x86)}\8x8 Inc\8x8 Work\8x8 Work.exe", "${env:ProgramFiles(x86)}\8x8 Inc\8x8 Work\8x8 Work.exe",
"${env:ProgramFiles}\8x8 Work\8x8 Work.exe", "${env:ProgramFiles}\8x8 Work\8x8 Work.exe",
"$env:LocalAppData\Programs\8x8 Work\8x8 Work.exe" "$env:LocalAppData\Programs\8x8 Work\8x8 Work.exe"
) )
$exePath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1 $exePath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($exePath) { if ($exePath) {
Start-Process -FilePath $exePath Start-Process -FilePath $exePath
Write-Host "8x8 Work launched successfully from: $exePath" -ForegroundColor Green Write-Host "8x8 Work launched successfully from: $exePath" -ForegroundColor Green
} else { } else {
Write-Warning "Could not locate 8x8 Work executable at 'C:\Program Files\8x8 Inc\8x8 Work\8x8 Work.exe' or fallback directories." Write-Warning "Could not locate 8x8 Work executable at 'C:\Program Files\8x8 Inc\8x8 Work\8x8 Work.exe' or fallback directories."
} }
} finally { } finally {
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Mandatory Cleanup: Deletes Scheduled Task & Temp Files # Mandatory Cleanup: Deletes Scheduled Task & Temp Files
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
Write-Host "Cleaning up scheduled task and temporary files..." -ForegroundColor Gray Write-Host "Cleaning up scheduled task and temporary files..." -ForegroundColor Gray
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
if (Test-Path $workDir) { if (Test-Path $workDir) {
Remove-Item $workDir -Recurse -Force -ErrorAction SilentlyContinue Remove-Item $workDir -Recurse -Force -ErrorAction SilentlyContinue
} }
} }