Update 8x8_Work_Updater.ps1
This commit is contained in:
+50
-259
@@ -1,8 +1,9 @@
|
|||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# Script Name: 8x8_Work_Updater.ps1
|
# Script Name: 8x8_Work_Updater.ps1
|
||||||
# Description: Self-contained console updater. Uses native Win32 token
|
# Description: Downloads 8x8 with a progress bar, schedules and launches a native
|
||||||
# privilege elevation to launch the interactive GUI on the
|
# VBScript interactive countdown directly in the user session,
|
||||||
# active desktop without triggering Defender or using external tools.
|
# waits for user confirmation or 1-hour timeout, silently installs,
|
||||||
|
# and relaunches 8x8 Work.
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
# Ensure script is running elevated
|
# Ensure script is running elevated
|
||||||
@@ -15,7 +16,7 @@ Add-Type -AssemblyName System.Net.Http
|
|||||||
|
|
||||||
$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"
|
$vbsScript = Join-Path $workDir "Show-8x8Prompt.vbs"
|
||||||
$installerPath = Join-Path $workDir "8x8_Work_Installer.msi"
|
$installerPath = Join-Path $workDir "8x8_Work_Installer.msi"
|
||||||
|
|
||||||
if (-not (Test-Path $workDir)) {
|
if (-not (Test-Path $workDir)) {
|
||||||
@@ -29,163 +30,9 @@ Set-Acl $workDir $acl
|
|||||||
|
|
||||||
if (Test-Path $flagFile) { Remove-Item $flagFile -Force }
|
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 {
|
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"
|
$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
|
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 = @'
|
$vbsContent = @"
|
||||||
Add-Type -AssemblyName System.Windows.Forms
|
Set objShell = CreateObject("WScript.Shell")
|
||||||
Add-Type -AssemblyName System.Drawing
|
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
|
' 64 = Information Icon, 4096 = System Modal (Always On Top)
|
||||||
$form.Text = "JCT600 IT Support — 8x8 Work Update"
|
intReturn = objShell.Popup(strMsg, 3600, "JCT600 IT Support - 8x8 Work Update", 64 + 4096)
|
||||||
$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
|
|
||||||
|
|
||||||
$script:allowClose = $false
|
' Create flag file when user clicks OK or when 3600 seconds expire
|
||||||
$form.Add_FormClosing({
|
Set objFile = objFSO.CreateTextFile("$flagFile", True)
|
||||||
param($sender, $e)
|
objFile.WriteLine("proceed")
|
||||||
if (-not $script:allowClose) { $e.Cancel = $true }
|
objFile.Close
|
||||||
})
|
"@
|
||||||
|
|
||||||
$labelHeader = New-Object System.Windows.Forms.Label
|
[System.IO.File]::WriteAllText($vbsScript, $vbsContent, [System.Text.Encoding]::ASCII)
|
||||||
$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)
|
|
||||||
|
|
||||||
$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."
|
# Step 3: Launch Native Dialog inside Active User Session
|
||||||
$labelMsg.Location = New-Object System.Drawing.Point(20, 48)
|
# --------------------------------------------------------------------------
|
||||||
$labelMsg.Size = New-Object System.Drawing.Size(400, 40)
|
Write-Host "[3/4] Launching prompt on logged-in user desktop..." -ForegroundColor Cyan
|
||||||
$labelMsg.Font = New-Object System.Drawing.Font("Segoe UI", 9)
|
|
||||||
$labelMsg.TextAlign = 'TopCenter'
|
|
||||||
$form.Controls.Add($labelMsg)
|
|
||||||
|
|
||||||
$labelTimer = New-Object System.Windows.Forms.Label
|
$loggedUser = (Get-CimInstance Win32_ComputerSystem).UserName
|
||||||
$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)
|
|
||||||
|
|
||||||
$btnUpdateNow = New-Object System.Windows.Forms.Button
|
if ([string]::IsNullOrWhiteSpace($loggedUser)) {
|
||||||
$btnUpdateNow.Text = "Update Now"
|
Write-Warning "No active interactive user detected. Proceeding with silent install."
|
||||||
$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--
|
|
||||||
} else {
|
} else {
|
||||||
$timer.Stop()
|
$taskName = "JCT600_8x8_UpdatePrompt"
|
||||||
$script:allowClose = $true
|
|
||||||
New-Item -Path $flagPath -ItemType File -Force | Out-Null
|
|
||||||
$form.Close()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
$initialSpan = [TimeSpan]::FromSeconds($script:remaining)
|
# Schedule task to run interactively under the logged-on user account
|
||||||
$labelTimer.Text = $initialSpan.ToString("hh\:mm\:ss")
|
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()
|
Write-Host "Prompt sent to $loggedUser. Waiting for user confirmation or 1-hour expiration..." -ForegroundColor Yellow
|
||||||
[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
|
|
||||||
while (-not (Test-Path $flagFile)) {
|
while (-not (Test-Path $flagFile)) {
|
||||||
Start-Sleep -Seconds 2
|
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 {
|
$targetProcesses = Get-Process | Where-Object {
|
||||||
$_.ProcessName -like "*8x8*" -or $_.MainWindowTitle -like "*8x8*"
|
$_.ProcessName -like "*8x8*" -or $_.MainWindowTitle -like "*8x8*"
|
||||||
@@ -361,10 +146,8 @@ $timer.Start()
|
|||||||
Write-Host "Installation completed successfully." -ForegroundColor Green
|
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 = @(
|
$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",
|
||||||
@@ -375,8 +158,16 @@ $timer.Start()
|
|||||||
$exePath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1
|
$exePath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1
|
||||||
|
|
||||||
if ($exePath) {
|
if ($exePath) {
|
||||||
[NativeDesktopLauncher]::RunAsActiveUser("`"$exePath`"") | Out-Null
|
if (-not [string]::IsNullOrWhiteSpace($loggedUser)) {
|
||||||
Write-Host "8x8 Work launched successfully in user context." -ForegroundColor Green
|
$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 {
|
} else {
|
||||||
Write-Warning "Could not locate 8x8 Work executable."
|
Write-Warning "Could not locate 8x8 Work executable."
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user