Skip to content

Commit

Permalink
[3] Admin elevation (#2601)
Browse files Browse the repository at this point in the history
* admin titus version

* add proper method to launch correct script

* fix

* add .exe

* last fix

* remove ! that was for testing

---------

Co-authored-by: Chris Titus <[email protected]>
  • Loading branch information
MyDrift-user and ChrisTitusTech authored Aug 28, 2024
1 parent 9ae5092 commit cc157f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions scripts/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ $sync.version = "#{replaceme}"
$sync.configs = @{}
$sync.ProcessRunning = $false

# If script isn't running as admin, show error message and quit
If (([Security.Principal.WindowsIdentity]::GetCurrent()).Owner.Value -ne "S-1-5-32-544") {
Write-Host "===========================================" -Foregroundcolor Red
Write-Host "-- Scripts must be run as Administrator ---" -Foregroundcolor Red
Write-Host "-- Right-Click Start -> Terminal(Admin) ---" -Foregroundcolor Red
Write-Host "===========================================" -Foregroundcolor Red
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."

$script = if ($MyInvocation.MyCommand.Path) { "& '" + $MyInvocation.MyCommand.Path + "'" } else { "irm 'https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1' | iex"}
$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
}

Expand Down
12 changes: 12 additions & 0 deletions windev.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
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."

$script = if ($MyInvocation.MyCommand.Path) { "& '" + $MyInvocation.MyCommand.Path + "'" } else { "irm 'https://github.com/ChrisTitusTech/winutil/raw/main/windev.ps1' | iex"}
$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 {
Expand Down

0 comments on commit cc157f6

Please sign in to comment.