mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2024-11-14 22:55:52 -06: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
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user