From c25b7ed4ac754816776df71c7c5f4c589152b6b3 Mon Sep 17 00:00:00 2001 From: Marterich <47688561+Marterich@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:56:17 +0200 Subject: [PATCH] Unify logic to reuse the same urls whether the script is started as admin or not --- windev.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/windev.ps1 b/windev.ps1 index 3e1805f0..2e48ab4f 100644 --- a/windev.ps1 +++ b/windev.ps1 @@ -34,7 +34,20 @@ function RedirectToLatestPreRelease { Write-Host "Using latest Full Release" $url = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1" } - Invoke-RestMethod $url | Invoke-Expression + + $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." + + $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