From 6a871b1feb681063fb593f3a8528eb98dc4d6e7f Mon Sep 17 00:00:00 2001 From: Underscore Date: Thu, 10 Oct 2024 15:12:39 -0700 Subject: [PATCH 1/4] Add -NoExit to $powershellcmd command within Start-Process argument list. --- scripts/start.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start.ps1 b/scripts/start.ps1 index c289709c..d00e3528 100644 --- a/scripts/start.ps1 +++ b/scripts/start.ps1 @@ -60,7 +60,7 @@ if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]: $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 + Start-Process $processCmd -ArgumentList "$powershellcmd -ExecutionPolicy Bypass -NoExit -NoProfile -Command $script" -Verb RunAs break } From 1bc32e0aff46b37eccea7fbae45c579e918f9e04 Mon Sep 17 00:00:00 2001 From: Underscore Date: Thu, 10 Oct 2024 15:27:39 -0700 Subject: [PATCH 2/4] Change irm to Invoke-RestMethod When scripting it's best to use the full function in Powershell. --- scripts/start.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start.ps1 b/scripts/start.ps1 index d00e3528..323a1c29 100644 --- a/scripts/start.ps1 +++ b/scripts/start.ps1 @@ -54,7 +54,7 @@ if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]: $script = if ($MyInvocation.MyCommand.Path) { "& { & '$($MyInvocation.MyCommand.Path)' $argList }" } else { - "iex '& { $(irm https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1) } $argList'" + "iex '& { $(Invoke-RestMethod https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1) } $argList'" } $powershellcmd = if (Get-Command pwsh -ErrorAction SilentlyContinue) { "pwsh" } else { "powershell" } From a9a6d7cf64a62b563cf7d2ca68283351db57ecab Mon Sep 17 00:00:00 2001 From: Underscore Date: Thu, 10 Oct 2024 18:59:51 -0700 Subject: [PATCH 3/4] Fixes for start.ps1 - Rollback changes from #2648 - Create Variable for script location. - irm -> Invoke-RestMethod, for Powershell standard coding practices. - iex -> Invoke-Expression, for Powershell standard coding practices. --- scripts/start.ps1 | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/scripts/start.ps1 b/scripts/start.ps1 index 323a1c29..dcc23865 100644 --- a/scripts/start.ps1 +++ b/scripts/start.ps1 @@ -39,28 +39,16 @@ $sync.version = "#{replaceme}" $sync.configs = @{} $sync.ProcessRunning = $false +$latestScript = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.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." - $argList = @() - - $PSBoundParameters.GetEnumerator() | ForEach-Object { - $argList += if ($_.Value -is [switch] -and $_.Value) { - "-$($_.Key)" - } elseif ($_.Value) { - "-$($_.Key) `"$($_.Value)`"" - } - } - - $script = if ($MyInvocation.MyCommand.Path) { - "& { & '$($MyInvocation.MyCommand.Path)' $argList }" - } else { - "iex '& { $(Invoke-RestMethod https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1) } $argList'" - } + $script = if ($MyInvocation.MyCommand.Path) { "& '" + $MyInvocation.MyCommand.Path + "'" } else { "Invoke-RestMethod '$latestScript' | Invoke-Expression"} $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 -NoExit -NoProfile -Command $script" -Verb RunAs + Start-Process $processCmd -ArgumentList "$powershellcmd -ExecutionPolicy Bypass -NoProfile -Command $script" -Verb RunAs break } From 31be3b01c6136da0ac5c650f7efd98712d9bee93 Mon Sep 17 00:00:00 2001 From: Underscore Date: Thu, 10 Oct 2024 19:09:54 -0700 Subject: [PATCH 4/4] Added comments. --- scripts/start.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/start.ps1 b/scripts/start.ps1 index dcc23865..c12b82b1 100644 --- a/scripts/start.ps1 +++ b/scripts/start.ps1 @@ -39,20 +39,26 @@ $sync.version = "#{replaceme}" $sync.configs = @{} $sync.ProcessRunning = $false +# Store latest script URL in variable. $latestScript = "https://github.com/ChrisTitusTech/winutil/releases/latest/download/winutil.ps1" +# Check if script is running as Administrator 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." + + # Partial rollback from #2648, changed irm and iex to Invoke-RestMethod and Invoke-Expression. $script = if ($MyInvocation.MyCommand.Path) { "& '" + $MyInvocation.MyCommand.Path + "'" } else { "Invoke-RestMethod '$latestScript' | Invoke-Expression"} $powershellcmd = if (Get-Command pwsh -ErrorAction SilentlyContinue) { "pwsh" } else { "powershell" } $processCmd = if (Get-Command wt.exe -ErrorAction SilentlyContinue) { "wt.exe" } else { $powershellcmd } + # Start new process with elevated privileges Start-Process $processCmd -ArgumentList "$powershellcmd -ExecutionPolicy Bypass -NoProfile -Command $script" -Verb RunAs break } +# Logging $dateTime = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $logdir = "$env:localappdata\winutil\logs"