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
This commit is contained in:
Martin Wiethan 2024-09-10 21:03:49 +02:00 committed by GitHub
parent 9abe11c975
commit ff363d686f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,25 +12,6 @@
Run in Admin Powershell > ./windev.ps1 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 to fetch the latest release tag from the GitHub API
function Get-LatestRelease { function Get-LatestRelease {
try { try {
@ -54,8 +35,21 @@ function RedirectToLatestPreRelease {
$url = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1" $url = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1"
} }
iex "& { $(irm $url) } $argList" $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 # Call the redirect function
RedirectToLatestPreRelease RedirectToLatestPreRelease