From ff363d686f8771e77aa9b2f234505f6c0d0103da Mon Sep 17 00:00:00 2001 From: Martin Wiethan <47688561+Marterich@users.noreply.github.com> Date: Tue, 10 Sep 2024 21:03:49 +0200 Subject: [PATCH] Fix/Refactor windev Admin Elevation (#2683) * Revert Admin Elevation * Unify logic to reuse the same urls whether the script is started as admin or not --- windev.ps1 | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/windev.ps1 b/windev.ps1 index b735efca..2e48ab4f 100644 --- a/windev.ps1 +++ b/windev.ps1 @@ -12,25 +12,6 @@ Run in Admin Powershell > ./windev.ps1 #> -if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { - Write-Output "Winutil needs to be run as Administrator. Attempting to relaunch." - # Capture all the arguments passed to the script - $argList = $args -join ' ' - - $script = if ($MyInvocation.MyCommand.Path) { - "& { & '$($MyInvocation.MyCommand.Path)' $argList }" - } else { - "iex '& { $(irm https://github.com/ChrisTitusTech/winutil/raw/main/windev.ps1) } $argList'" - } - - $powershellcmd = if (Get-Command pwsh -ErrorAction SilentlyContinue) { "pwsh" } else { "powershell" } - $processCmd = if (Get-Command wt.exe -ErrorAction SilentlyContinue) { "wt.exe" } else { $powershellcmd } - - Start-Process $processCmd -ArgumentList "$powershellcmd -ExecutionPolicy Bypass -NoProfile -Command $script" -Verb RunAs - - break -} - # Function to fetch the latest release tag from the GitHub API function Get-LatestRelease { try { @@ -53,9 +34,22 @@ function RedirectToLatestPreRelease { Write-Host "Using latest Full Release" $url = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1" } + + $script = Invoke-RestMethod $url + # Elevate Shell if necessary + if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Output "Winutil needs to be run as Administrator. Attempting to relaunch." - iex "& { $(irm $url) } $argList" + $powershellcmd = if (Get-Command pwsh -ErrorAction SilentlyContinue) { "pwsh" } else { "powershell" } + $processCmd = if (Get-Command wt.exe -ErrorAction SilentlyContinue) { "wt.exe" } else { $powershellcmd } + + Start-Process $processCmd -ArgumentList "$powershellcmd -ExecutionPolicy Bypass -NoProfile -Command $(Invoke-Expression $script)" -Verb RunAs + } + else{ + Invoke-Expression $script + } } # Call the redirect function + RedirectToLatestPreRelease