Skip to content

Instantly share code, notes, and snippets.

@davidlu1001
Created July 25, 2024 11:33
Show Gist options
  • Select an option

  • Save davidlu1001/ab9b6b7b6b9d09ee12472bad3be4591f to your computer and use it in GitHub Desktop.

Select an option

Save davidlu1001/ab9b6b7b6b9d09ee12472bad3be4591f to your computer and use it in GitHub Desktop.
Install EdgeEnterprise
<#
.SYNOPSIS
Installs or updates Microsoft Edge Enterprise x64 package.
.DESCRIPTION
This script checks the local Edge version, compares it with the downloaded version,
and installs the new version if it's newer. It handles process termination and various installation scenarios.
.PARAMETER InstallerPath
The path to the Microsoft Edge MSI installer. Default is "C:\temp\scripts\MicrosoftEdgeEnterpriseX64.msi".
.EXAMPLE
.\Install-EdgeEnterprise.ps1
.\Install-EdgeEnterprise.ps1 -InstallerPath "D:\Downloads\MicrosoftEdgeEnterpriseX64.msi" -Verbose
.NOTES
Requires PowerShell 5.1 or later and administrative privileges.
Author: Assistant
Date: Current Date
#>
[CmdletBinding()]
param (
[string]$InstallerPath = "C:\temp\scripts\MicrosoftEdgeEnterpriseX64.msi"
)
# Function to write log messages
function Write-Log {
param (
[string]$Message,
[string]$Severity = "Information"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Severity] $Message"
switch ($Severity) {
"Information" {
Write-Verbose $logMessage
if (-not $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
Write-Host $logMessage
}
}
"Warning" { Write-Warning $logMessage }
"Error" { Write-Error $logMessage }
}
}
# Function to get Edge version
function Get-EdgeVersion {
try {
$edgePath = "${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe"
if (Test-Path $edgePath) {
$version = (Get-Item $edgePath).VersionInfo.FileVersion
return [version]$version
}
}
catch {
Write-Log "Error getting Edge version: $_" -Severity "Error"
}
return $null
}
# Function to get MSI file version
function Get-MsiFileVersion {
param ([string]$FilePath)
try {
$windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $null, $windowsInstaller, @($FilePath, 0))
$query = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
$view = $database.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $database, ($query))
$view.GetType().InvokeMember("Execute", "InvokeMethod", $null, $view, $null)
$record = $view.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $view, $null)
$version = $record.GetType().InvokeMember("StringData", "GetProperty", $null, $record, 1)
$view.GetType().InvokeMember("Close", "InvokeMethod", $null, $view, $null)
$database.GetType().InvokeMember("Commit", "InvokeMethod", $null, $database, $null)
return [version]$version
}
catch {
Write-Log "Error getting MSI file version: $_" -Severity "Error"
return $null
}
finally {
if ($null -ne $windowsInstaller) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($windowsInstaller) | Out-Null
}
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
}
# Function to close Edge processes
function Close-EdgeProcesses {
$edgeProcesses = Get-Process msedge -ErrorAction SilentlyContinue
if ($edgeProcesses) {
Write-Log "Closing Microsoft Edge processes..."
foreach ($process in $edgeProcesses) {
try {
$process.CloseMainWindow() | Out-Null
if (!$process.HasExited) {
Start-Sleep -Seconds 5
$process | Stop-Process -Force
}
}
catch {
Write-Log "Error closing Edge process (ID: $($process.Id)): $_" -Severity "Warning"
}
}
Start-Sleep -Seconds 2 # Wait a bit to ensure all processes are closed
}
}
# Function to install MSI
function Install-MsiPackage {
param ([string]$MsiPath)
$logFile = "$env:TEMP\EdgeInstall_$(Get-Date -Format 'yyyyMMddHHmmss').log"
$arguments = "/i `"$MsiPath`" /qn /norestart /l*v `"$logFile`""
try {
$process = Start-Process msiexec.exe -ArgumentList $arguments -Wait -PassThru -NoNewWindow
if ($process.ExitCode -eq 0) {
Write-Log "Installation completed successfully."
return $true
}
else {
Write-Log "Installation failed with exit code: $($process.ExitCode). Check log: $logFile" -Severity "Error"
return $false
}
}
catch {
Write-Log "Error during installation: $_" -Severity "Error"
return $false
}
}
# Main script logic
try {
# Check if running with admin privileges
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
throw "This script must be run as Administrator"
}
# Verify installer exists
if (-not (Test-Path $InstallerPath)) {
throw "Installer not found at path: $InstallerPath"
}
$localVersion = Get-EdgeVersion
$installerVersion = Get-MsiFileVersion -FilePath $InstallerPath
if ($null -eq $installerVersion) {
throw "Unable to determine the version of the installer"
}
Write-Log "Local Edge version: $localVersion"
Write-Log "Installer version: $installerVersion"
if ($null -eq $localVersion -or $installerVersion -gt $localVersion) {
Write-Log "New version available. Proceeding with installation."
Close-EdgeProcesses
$installSuccess = Install-MsiPackage -MsiPath $InstallerPath
if ($installSuccess) {
$newVersion = Get-EdgeVersion
Write-Log "New Edge version installed: $newVersion"
}
}
else {
Write-Log "Local version is up to date. No installation needed."
}
}
catch {
Write-Log "An error occurred: $_" -Severity "Error"
exit 1
}
finally {
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment