mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-11-24 11:55:31 +00:00
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:
parent
9abe11c975
commit
ff363d686f
34
windev.ps1
34
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
|
||||
|
Loading…
Reference in New Issue
Block a user