From 73fe76758eb5dfe3daf1301ce827e456a292c0f1 Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:29:43 -0700 Subject: [PATCH 1/7] Create pwsh-run.ps1 --- pwsh-run.ps1 | 1 + 1 file changed, 1 insertion(+) create mode 100644 pwsh-run.ps1 diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pwsh-run.ps1 @@ -0,0 +1 @@ + From dd94d48307702b3e2492eb03c16fde60227634a0 Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:34:41 -0700 Subject: [PATCH 2/7] Update pwsh-run.ps1 --- pwsh-run.ps1 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 index 8b13789..3e5bd8d 100644 --- a/pwsh-run.ps1 +++ b/pwsh-run.ps1 @@ -1 +1,30 @@ +# Ensure running as admin +$windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() +$windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) +$isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +if (-not $isAdmin) { + Write-Host "Not running as admin. Relaunching as administrator..." + Start-Process powershell "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs + exit +} + +# Set repo and .bat filename +$repoOwner = "Batlez" +$repoName = "Batlez-Tweaks" +$batFileName = "Batlez Tweaks.bat" +$branch = "main" # Change if needed + +# Download the .bat file to temp +$tempBat = Join-Path $env:TEMP $batFileName +$encodedBatName = [uri]::EscapeDataString($batFileName) +$rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$encodedBatName" +Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat + +# Execute the .bat file +Write-Host "Running $batFileName as administrator..." +Start-Process -FilePath $tempBat -Verb RunAs -Wait + +# Cleanup +Remove-Item $tempBat -Force +Write-Host "Done!" From c77b50483c223c1afa952fe6774fb371f2e9e9f5 Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:38:21 -0700 Subject: [PATCH 3/7] Update pwsh-run.ps1 --- pwsh-run.ps1 | 184 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 158 insertions(+), 26 deletions(-) diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 index 3e5bd8d..8b6844a 100644 --- a/pwsh-run.ps1 +++ b/pwsh-run.ps1 @@ -1,30 +1,162 @@ -# Ensure running as admin -$windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() -$windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) -$isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +# Enhanced script with debugging and error handling +param( + [switch]$Debug = $false +) -if (-not $isAdmin) { - Write-Host "Not running as admin. Relaunching as administrator..." - Start-Process powershell "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs - exit +# Function to write timestamped logs +function Write-Log { + param([string]$Message, [string]$Level = "INFO") + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $logMessage = "[$timestamp] [$Level] $Message" + Write-Host $logMessage + + # Also write to a log file for persistence + $logFile = Join-Path $env:TEMP "batlez-tweaks-debug.log" + Add-Content -Path $logFile -Value $logMessage -ErrorAction SilentlyContinue } -# Set repo and .bat filename -$repoOwner = "Batlez" -$repoName = "Batlez-Tweaks" -$batFileName = "Batlez Tweaks.bat" -$branch = "main" # Change if needed - -# Download the .bat file to temp -$tempBat = Join-Path $env:TEMP $batFileName -$encodedBatName = [uri]::EscapeDataString($batFileName) -$rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$encodedBatName" -Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat - -# Execute the .bat file -Write-Host "Running $batFileName as administrator..." -Start-Process -FilePath $tempBat -Verb RunAs -Wait +# Function to pause execution (useful for debugging) +function Pause-Execution { + param([string]$Message = "Press any key to continue...") + if ($Debug) { + Write-Log $Message "DEBUG" + Read-Host $Message + } +} -# Cleanup -Remove-Item $tempBat -Force -Write-Host "Done!" +try { + Write-Log "Script started. Debug mode: $Debug" + Write-Log "Current user: $($env:USERNAME)" + Write-Log "Current directory: $(Get-Location)" + + # Ensure running as admin + $windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() + $windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) + $isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + + Write-Log "Admin check result: $isAdmin" + + if (-not $isAdmin) { + Write-Log "Not running as admin. Relaunching as administrator..." "WARN" + + # Get the current script path + if ($PSCommandPath) { + $scriptPath = $PSCommandPath + Write-Log "Using PSCommandPath: $scriptPath" + } else { + # Fallback for when running via iex + Write-Log "PSCommandPath not available (likely running via iex). Creating temp script..." + $scriptContent = Get-Content $MyInvocation.MyCommand.Path -Raw -ErrorAction SilentlyContinue + if (-not $scriptContent) { + # If we can't get the script content, re-download it + $tempScript = Join-Path $env:TEMP "batlez-tweaks-temp.ps1" + $scriptUrl = "https://raw.githubusercontent.com/M1noa/Batlez-Tweaks/main/pwsh-run.ps1" + Write-Log "Re-downloading script to: $tempScript" + Invoke-WebRequest -Uri $scriptUrl -OutFile $tempScript -ErrorAction Stop + $scriptPath = $tempScript + } + } + + $debugParam = if ($Debug) { "-Debug" } else { "" } + $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`" $debugParam" + Write-Log "Launching with arguments: $arguments" + + Start-Process powershell $arguments -Verb RunAs + Write-Log "Admin process launched, exiting current process" + exit + } + + Write-Log "Running as administrator - proceeding with main logic" + Pause-Execution "About to start downloading and executing the batch file..." + + # Set repo and .bat filename + $repoOwner = "M1noa" # Fixed: was "Batlez" but you're using M1noa fork + $repoName = "Batlez-Tweaks" + $batFileName = "Batlez Tweaks.bat" + $branch = "main" + + Write-Log "Repository: $repoOwner/$repoName" + Write-Log "Branch: $branch" + Write-Log "Batch file: $batFileName" + + # Download the .bat file to temp + $tempBat = Join-Path $env:TEMP $batFileName + $rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$([uri]::EscapeDataString($batFileName))" + + Write-Log "Download URL: $rawUrl" + Write-Log "Temp file location: $tempBat" + + Write-Log "Downloading batch file..." "INFO" + try { + Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat -ErrorAction Stop + Write-Log "Download completed successfully" + + # Verify file was downloaded + if (Test-Path $tempBat) { + $fileSize = (Get-Item $tempBat).Length + Write-Log "File downloaded successfully. Size: $fileSize bytes" + } else { + throw "Downloaded file not found at expected location" + } + } + catch { + Write-Log "Download failed: $($_.Exception.Message)" "ERROR" + throw + } + + Pause-Execution "About to execute the batch file..." + + # Execute the .bat file + Write-Log "Running $batFileName as administrator..." + try { + $process = Start-Process -FilePath $tempBat -Verb RunAs -Wait -PassThru -ErrorAction Stop + Write-Log "Batch file execution completed with exit code: $($process.ExitCode)" + } + catch { + Write-Log "Failed to execute batch file: $($_.Exception.Message)" "ERROR" + throw + } + + # Cleanup + Write-Log "Cleaning up temporary files..." + try { + if (Test-Path $tempBat) { + Remove-Item $tempBat -Force -ErrorAction Stop + Write-Log "Temporary batch file deleted" + } + + # Clean up temp script if it was created + $tempScript = Join-Path $env:TEMP "batlez-tweaks-temp.ps1" + if (Test-Path $tempScript) { + Remove-Item $tempScript -Force -ErrorAction SilentlyContinue + Write-Log "Temporary PowerShell script deleted" + } + } + catch { + Write-Log "Cleanup warning: $($_.Exception.Message)" "WARN" + } + + Write-Log "Script completed successfully!" "SUCCESS" +} +catch { + Write-Log "FATAL ERROR: $($_.Exception.Message)" "ERROR" + Write-Log "Stack trace: $($_.ScriptStackTrace)" "ERROR" + + if ($Debug) { + Write-Log "Error details:" "ERROR" + Write-Log "Exception Type: $($_.Exception.GetType().FullName)" "ERROR" + Write-Log "Inner Exception: $($_.Exception.InnerException)" "ERROR" + } +} +finally { + if ($Debug -or $isAdmin) { + Write-Log "Press any key to close this window..." + Read-Host "Press Enter to exit" + } + + # Show log file location + $logFile = Join-Path $env:TEMP "batlez-tweaks-debug.log" + if (Test-Path $logFile) { + Write-Log "Debug log saved to: $logFile" + } +} From 2942ff9aa097b3f92dc716ab70ada6526e3340ed Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:41:35 -0700 Subject: [PATCH 4/7] Update pwsh-run.ps1 --- pwsh-run.ps1 | 250 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 179 insertions(+), 71 deletions(-) diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 index 8b6844a..4195447 100644 --- a/pwsh-run.ps1 +++ b/pwsh-run.ps1 @@ -1,6 +1,7 @@ -# Enhanced script with debugging and error handling +# Enhanced script with improved elevation system param( - [switch]$Debug = $false + [switch]$Debug = $false, + [switch]$ElevatedRun = $false ) # Function to write timestamped logs @@ -24,118 +25,222 @@ function Pause-Execution { } } -try { - Write-Log "Script started. Debug mode: $Debug" - Write-Log "Current user: $($env:USERNAME)" - Write-Log "Current directory: $(Get-Location)" +# Function to create and run elevated script +function Start-ElevatedScript { + param([bool]$DebugMode = $false) - # Ensure running as admin - $windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() - $windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) - $isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + Write-Log "Creating elevated script for admin execution..." - Write-Log "Admin check result: $isAdmin" + # Create the full script content as a string + $elevatedScriptContent = @" +# This is the elevated version of the script +`$Debug = `$$DebugMode + +# Function to write timestamped logs +function Write-Log { + param([string]`$Message, [string]`$Level = "INFO") + `$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + `$logMessage = "[`$timestamp] [`$Level] `$Message" + Write-Host `$logMessage - if (-not $isAdmin) { - Write-Log "Not running as admin. Relaunching as administrator..." "WARN" - - # Get the current script path - if ($PSCommandPath) { - $scriptPath = $PSCommandPath - Write-Log "Using PSCommandPath: $scriptPath" - } else { - # Fallback for when running via iex - Write-Log "PSCommandPath not available (likely running via iex). Creating temp script..." - $scriptContent = Get-Content $MyInvocation.MyCommand.Path -Raw -ErrorAction SilentlyContinue - if (-not $scriptContent) { - # If we can't get the script content, re-download it - $tempScript = Join-Path $env:TEMP "batlez-tweaks-temp.ps1" - $scriptUrl = "https://raw.githubusercontent.com/M1noa/Batlez-Tweaks/main/pwsh-run.ps1" - Write-Log "Re-downloading script to: $tempScript" - Invoke-WebRequest -Uri $scriptUrl -OutFile $tempScript -ErrorAction Stop - $scriptPath = $tempScript - } - } - - $debugParam = if ($Debug) { "-Debug" } else { "" } - $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`" $debugParam" - Write-Log "Launching with arguments: $arguments" - - Start-Process powershell $arguments -Verb RunAs - Write-Log "Admin process launched, exiting current process" - exit + # Also write to a log file for persistence + `$logFile = Join-Path `$env:TEMP "batlez-tweaks-debug.log" + Add-Content -Path `$logFile -Value `$logMessage -ErrorAction SilentlyContinue +} + +# Function to pause execution (useful for debugging) +function Pause-Execution { + param([string]`$Message = "Press any key to continue...") + if (`$Debug) { + Write-Log `$Message "DEBUG" + Read-Host `$Message } - - Write-Log "Running as administrator - proceeding with main logic" - Pause-Execution "About to start downloading and executing the batch file..." +} + +try { + Write-Log "Elevated script started. Debug mode: `$Debug" + Write-Log "Current user: `$(`$env:USERNAME)" + Write-Log "Running as administrator: `$([Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))" # Set repo and .bat filename - $repoOwner = "M1noa" # Fixed: was "Batlez" but you're using M1noa fork - $repoName = "Batlez-Tweaks" - $batFileName = "Batlez Tweaks.bat" - $branch = "main" + `$repoOwner = "Batlez" + `$repoName = "Batlez-Tweaks" + `$batFileName = "Batlez Tweaks.bat" + `$branch = "main" - Write-Log "Repository: $repoOwner/$repoName" - Write-Log "Branch: $branch" - Write-Log "Batch file: $batFileName" + Write-Log "Repository: `$repoOwner/`$repoName" + Write-Log "Branch: `$branch" + Write-Log "Batch file: `$batFileName" # Download the .bat file to temp - $tempBat = Join-Path $env:TEMP $batFileName - $rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$([uri]::EscapeDataString($batFileName))" + `$tempBat = Join-Path `$env:TEMP `$batFileName + `$rawUrl = "https://raw.githubusercontent.com/`$repoOwner/`$repoName/`$branch/`$([uri]::EscapeDataString(`$batFileName))" - Write-Log "Download URL: $rawUrl" - Write-Log "Temp file location: $tempBat" + Write-Log "Download URL: `$rawUrl" + Write-Log "Temp file location: `$tempBat" + + Pause-Execution "About to start downloading the batch file..." Write-Log "Downloading batch file..." "INFO" try { - Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat -ErrorAction Stop + Invoke-WebRequest -Uri `$rawUrl -OutFile `$tempBat -ErrorAction Stop Write-Log "Download completed successfully" # Verify file was downloaded - if (Test-Path $tempBat) { - $fileSize = (Get-Item $tempBat).Length - Write-Log "File downloaded successfully. Size: $fileSize bytes" + if (Test-Path `$tempBat) { + `$fileSize = (Get-Item `$tempBat).Length + Write-Log "File downloaded successfully. Size: `$fileSize bytes" + + if (`$fileSize -eq 0) { + throw "Downloaded file is empty (0 bytes)" + } } else { throw "Downloaded file not found at expected location" } } catch { - Write-Log "Download failed: $($_.Exception.Message)" "ERROR" + Write-Log "Download failed: `$(`$_.Exception.Message)" "ERROR" throw } Pause-Execution "About to execute the batch file..." # Execute the .bat file - Write-Log "Running $batFileName as administrator..." + Write-Log "Running `$batFileName as administrator..." try { - $process = Start-Process -FilePath $tempBat -Verb RunAs -Wait -PassThru -ErrorAction Stop - Write-Log "Batch file execution completed with exit code: $($process.ExitCode)" + `$process = Start-Process -FilePath `$tempBat -Verb RunAs -Wait -PassThru -ErrorAction Stop + Write-Log "Batch file execution completed with exit code: `$(`$process.ExitCode)" } catch { - Write-Log "Failed to execute batch file: $($_.Exception.Message)" "ERROR" + Write-Log "Failed to execute batch file: `$(`$_.Exception.Message)" "ERROR" throw } # Cleanup Write-Log "Cleaning up temporary files..." try { - if (Test-Path $tempBat) { - Remove-Item $tempBat -Force -ErrorAction Stop + if (Test-Path `$tempBat) { + Remove-Item `$tempBat -Force -ErrorAction Stop Write-Log "Temporary batch file deleted" } + } + catch { + Write-Log "Cleanup warning: `$(`$_.Exception.Message)" "WARN" + } + + Write-Log "Script completed successfully!" "SUCCESS" +} +catch { + Write-Log "FATAL ERROR: `$(`$_.Exception.Message)" "ERROR" + Write-Log "Stack trace: `$(`$_.ScriptStackTrace)" "ERROR" + + if (`$Debug) { + Write-Log "Error details:" "ERROR" + Write-Log "Exception Type: `$(`$_.Exception.GetType().FullName)" "ERROR" + Write-Log "Inner Exception: `$(`$_.Exception.InnerException)" "ERROR" + } +} +finally { + if (`$Debug) { + Write-Log "Press any key to close this window..." + Read-Host "Press Enter to exit" + } else { + # Give user a moment to see the results + Start-Sleep -Seconds 3 + } + + # Show log file location + `$logFile = Join-Path `$env:TEMP "batlez-tweaks-debug.log" + if (Test-Path `$logFile) { + Write-Log "Debug log saved to: `$logFile" + } +} +"@ + + # Write the script to a temporary file + $elevatedScriptPath = Join-Path $env:TEMP "batlez-tweaks-elevated.ps1" + try { + $elevatedScriptContent | Out-File -FilePath $elevatedScriptPath -Encoding UTF8 -Force + Write-Log "Elevated script written to: $elevatedScriptPath" - # Clean up temp script if it was created - $tempScript = Join-Path $env:TEMP "batlez-tweaks-temp.ps1" - if (Test-Path $tempScript) { - Remove-Item $tempScript -Force -ErrorAction SilentlyContinue - Write-Log "Temporary PowerShell script deleted" + # Launch the elevated script + $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$elevatedScriptPath`"" + Write-Log "Launching elevated PowerShell with arguments: $arguments" + + Start-Process powershell $arguments -Verb RunAs -Wait + Write-Log "Elevated process completed" + + # Cleanup the temporary elevated script + if (Test-Path $elevatedScriptPath) { + Remove-Item $elevatedScriptPath -Force -ErrorAction SilentlyContinue + Write-Log "Temporary elevated script cleaned up" } } catch { - Write-Log "Cleanup warning: $($_.Exception.Message)" "WARN" + Write-Log "Failed to create or run elevated script: $($_.Exception.Message)" "ERROR" + throw + } +} + +# Main script execution +try { + Write-Log "Script started. Debug mode: $Debug, ElevatedRun: $ElevatedRun" + Write-Log "Current user: $($env:USERNAME)" + Write-Log "Current directory: $(Get-Location)" + Write-Log "PowerShell version: $($PSVersionTable.PSVersion)" + + # Check if we're already running as admin + $windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() + $windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) + $isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + + Write-Log "Admin check result: $isAdmin" + + if (-not $isAdmin) { + Write-Log "Not running as admin. Starting elevation process..." "WARN" + Pause-Execution "About to request administrator privileges..." + + # Use the new elevation system + Start-ElevatedScript -DebugMode $Debug + + Write-Log "Elevation process completed, exiting current process" + exit } + Write-Log "Already running as administrator - this shouldn't happen with the new system" "WARN" + Write-Log "Proceeding with main logic anyway..." + + # If we somehow get here (running as admin), execute the main logic + # Set repo and .bat filename + $repoOwner = "Batlez" + $repoName = "Batlez-Tweaks" + $batFileName = "Batlez Tweaks.bat" + $branch = "main" + + Write-Log "Repository: $repoOwner/$repoName" + Write-Log "Branch: $branch" + Write-Log "Batch file: $batFileName" + + # Download the .bat file to temp + $tempBat = Join-Path $env:TEMP $batFileName + $rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$([uri]::EscapeDataString($batFileName))" + + Write-Log "Download URL: $rawUrl" + Write-Log "Temp file location: $tempBat" + + Write-Log "Downloading batch file..." "INFO" + Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat -ErrorAction Stop + Write-Log "Download completed successfully" + + # Execute the .bat file + Write-Log "Running $batFileName as administrator..." + $process = Start-Process -FilePath $tempBat -Verb RunAs -Wait -PassThru + Write-Log "Batch file execution completed with exit code: $($process.ExitCode)" + + # Cleanup + Remove-Item $tempBat -Force -ErrorAction SilentlyContinue + Write-Log "Cleanup completed" + Write-Log "Script completed successfully!" "SUCCESS" } catch { @@ -149,7 +254,7 @@ catch { } } finally { - if ($Debug -or $isAdmin) { + if ($Debug) { Write-Log "Press any key to close this window..." Read-Host "Press Enter to exit" } @@ -158,5 +263,8 @@ finally { $logFile = Join-Path $env:TEMP "batlez-tweaks-debug.log" if (Test-Path $logFile) { Write-Log "Debug log saved to: $logFile" + if ($Debug) { + Write-Log "You can view the full log with: notepad `"$logFile`"" + } } } From 7f36d2f43f9981b6ec21c1165078462147bdb123 Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:44:53 -0700 Subject: [PATCH 5/7] Update pwsh-run.ps1 --- pwsh-run.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 index 4195447..b65a62b 100644 --- a/pwsh-run.ps1 +++ b/pwsh-run.ps1 @@ -60,7 +60,10 @@ function Pause-Execution { try { Write-Log "Elevated script started. Debug mode: `$Debug" Write-Log "Current user: `$(`$env:USERNAME)" - Write-Log "Running as administrator: `$([Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))" + `$windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() + `$windowsPrincipal = New-Object Security.Principal.WindowsPrincipal(`$windowsIdentity) + `$isAdminCheck = `$windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + Write-Log "Running as administrator: `$isAdminCheck" # Set repo and .bat filename `$repoOwner = "Batlez" From 2fffe08df7f33e9c205f853c25270c177b7317ee Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:46:39 -0700 Subject: [PATCH 6/7] Update pwsh-run.ps1\ From b845a20c902192fef8e1fb8bb69fe2e9eaa21c1f Mon Sep 17 00:00:00 2001 From: Minoa <57844837+M1noa@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:56:32 -0700 Subject: [PATCH 7/7] i think imm donee --- pwsh-run.ps1 | 292 ++++++--------------------------------------------- 1 file changed, 31 insertions(+), 261 deletions(-) diff --git a/pwsh-run.ps1 b/pwsh-run.ps1 index b65a62b..3794a2e 100644 --- a/pwsh-run.ps1 +++ b/pwsh-run.ps1 @@ -1,273 +1,43 @@ -# Enhanced script with improved elevation system -param( - [switch]$Debug = $false, - [switch]$ElevatedRun = $false -) +# PowerShell Runner for Batlez Tweaks +# Usage: iwr -useb https://raw.githubusercontent.com/Batlez/Batlez-Tweaks/main/pwsh-run.ps1 | iex -# Function to write timestamped logs -function Write-Log { - param([string]$Message, [string]$Level = "INFO") - $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" - $logMessage = "[$timestamp] [$Level] $Message" - Write-Host $logMessage +# Check if running as administrator +if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Host "Requesting administrator privileges..." -ForegroundColor Yellow - # Also write to a log file for persistence - $logFile = Join-Path $env:TEMP "batlez-tweaks-debug.log" - Add-Content -Path $logFile -Value $logMessage -ErrorAction SilentlyContinue -} - -# Function to pause execution (useful for debugging) -function Pause-Execution { - param([string]$Message = "Press any key to continue...") - if ($Debug) { - Write-Log $Message "DEBUG" - Read-Host $Message - } -} - -# Function to create and run elevated script -function Start-ElevatedScript { - param([bool]$DebugMode = $false) - - Write-Log "Creating elevated script for admin execution..." + # Create temporary elevated script in Documents folder (safer than temp) + $elevatedScript = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "batlez-tweaks-runner.ps1" - # Create the full script content as a string - $elevatedScriptContent = @" -# This is the elevated version of the script -`$Debug = `$$DebugMode - -# Function to write timestamped logs -function Write-Log { - param([string]`$Message, [string]`$Level = "INFO") - `$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" - `$logMessage = "[`$timestamp] [`$Level] `$Message" - Write-Host `$logMessage - - # Also write to a log file for persistence - `$logFile = Join-Path `$env:TEMP "batlez-tweaks-debug.log" - Add-Content -Path `$logFile -Value `$logMessage -ErrorAction SilentlyContinue -} - -# Function to pause execution (useful for debugging) -function Pause-Execution { - param([string]`$Message = "Press any key to continue...") - if (`$Debug) { - Write-Log `$Message "DEBUG" - Read-Host `$Message - } -} + @' +$batchFile = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "Batlez-Tweaks.bat" +$url = "https://raw.githubusercontent.com/Batlez/Batlez-Tweaks/main/Batlez%20Tweaks.bat" try { - Write-Log "Elevated script started. Debug mode: `$Debug" - Write-Log "Current user: `$(`$env:USERNAME)" - `$windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() - `$windowsPrincipal = New-Object Security.Principal.WindowsPrincipal(`$windowsIdentity) - `$isAdminCheck = `$windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - Write-Log "Running as administrator: `$isAdminCheck" - - # Set repo and .bat filename - `$repoOwner = "Batlez" - `$repoName = "Batlez-Tweaks" - `$batFileName = "Batlez Tweaks.bat" - `$branch = "main" - - Write-Log "Repository: `$repoOwner/`$repoName" - Write-Log "Branch: `$branch" - Write-Log "Batch file: `$batFileName" - - # Download the .bat file to temp - `$tempBat = Join-Path `$env:TEMP `$batFileName - `$rawUrl = "https://raw.githubusercontent.com/`$repoOwner/`$repoName/`$branch/`$([uri]::EscapeDataString(`$batFileName))" - - Write-Log "Download URL: `$rawUrl" - Write-Log "Temp file location: `$tempBat" - - Pause-Execution "About to start downloading the batch file..." - - Write-Log "Downloading batch file..." "INFO" - try { - Invoke-WebRequest -Uri `$rawUrl -OutFile `$tempBat -ErrorAction Stop - Write-Log "Download completed successfully" - - # Verify file was downloaded - if (Test-Path `$tempBat) { - `$fileSize = (Get-Item `$tempBat).Length - Write-Log "File downloaded successfully. Size: `$fileSize bytes" - - if (`$fileSize -eq 0) { - throw "Downloaded file is empty (0 bytes)" - } - } else { - throw "Downloaded file not found at expected location" - } - } - catch { - Write-Log "Download failed: `$(`$_.Exception.Message)" "ERROR" - throw - } - - Pause-Execution "About to execute the batch file..." - - # Execute the .bat file - Write-Log "Running `$batFileName as administrator..." - try { - `$process = Start-Process -FilePath `$tempBat -Verb RunAs -Wait -PassThru -ErrorAction Stop - Write-Log "Batch file execution completed with exit code: `$(`$process.ExitCode)" - } - catch { - Write-Log "Failed to execute batch file: `$(`$_.Exception.Message)" "ERROR" - throw - } - - # Cleanup - Write-Log "Cleaning up temporary files..." - try { - if (Test-Path `$tempBat) { - Remove-Item `$tempBat -Force -ErrorAction Stop - Write-Log "Temporary batch file deleted" - } - } - catch { - Write-Log "Cleanup warning: `$(`$_.Exception.Message)" "WARN" - } - - Write-Log "Script completed successfully!" "SUCCESS" + Invoke-WebRequest -Uri $url -OutFile $batchFile -ErrorAction Stop + Start-Process -FilePath $batchFile -Wait + Remove-Item $batchFile -Force -ErrorAction SilentlyContinue + Remove-Item $PSCommandPath -Force -ErrorAction SilentlyContinue +} catch { + Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red + Read-Host "Press Enter to exit" } -catch { - Write-Log "FATAL ERROR: `$(`$_.Exception.Message)" "ERROR" - Write-Log "Stack trace: `$(`$_.ScriptStackTrace)" "ERROR" +'@ | Out-File -FilePath $elevatedScript -Encoding UTF8 -Force - if (`$Debug) { - Write-Log "Error details:" "ERROR" - Write-Log "Exception Type: `$(`$_.Exception.GetType().FullName)" "ERROR" - Write-Log "Inner Exception: `$(`$_.Exception.InnerException)" "ERROR" - } -} -finally { - if (`$Debug) { - Write-Log "Press any key to close this window..." - Read-Host "Press Enter to exit" - } else { - # Give user a moment to see the results - Start-Sleep -Seconds 3 - } + Start-Process powershell "-NoProfile -ExecutionPolicy Bypass -File `"$elevatedScript`"" -Verb RunAs - # Show log file location - `$logFile = Join-Path `$env:TEMP "batlez-tweaks-debug.log" - if (Test-Path `$logFile) { - Write-Log "Debug log saved to: `$logFile" - } -} -"@ - - # Write the script to a temporary file - $elevatedScriptPath = Join-Path $env:TEMP "batlez-tweaks-elevated.ps1" - try { - $elevatedScriptContent | Out-File -FilePath $elevatedScriptPath -Encoding UTF8 -Force - Write-Log "Elevated script written to: $elevatedScriptPath" - - # Launch the elevated script - $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$elevatedScriptPath`"" - Write-Log "Launching elevated PowerShell with arguments: $arguments" - - Start-Process powershell $arguments -Verb RunAs -Wait - Write-Log "Elevated process completed" - - # Cleanup the temporary elevated script - if (Test-Path $elevatedScriptPath) { - Remove-Item $elevatedScriptPath -Force -ErrorAction SilentlyContinue - Write-Log "Temporary elevated script cleaned up" - } - } - catch { - Write-Log "Failed to create or run elevated script: $($_.Exception.Message)" "ERROR" - throw - } + # Clean up after 10 seconds + Start-Job { Start-Sleep 10; Remove-Item $args[0] -Force -ErrorAction SilentlyContinue } -ArgumentList $elevatedScript | Out-Null + exit } -# Main script execution +# If already admin, run directly try { - Write-Log "Script started. Debug mode: $Debug, ElevatedRun: $ElevatedRun" - Write-Log "Current user: $($env:USERNAME)" - Write-Log "Current directory: $(Get-Location)" - Write-Log "PowerShell version: $($PSVersionTable.PSVersion)" - - # Check if we're already running as admin - $windowsIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() - $windowsPrincipal = New-Object Security.Principal.WindowsPrincipal($windowsIdentity) - $isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) - - Write-Log "Admin check result: $isAdmin" - - if (-not $isAdmin) { - Write-Log "Not running as admin. Starting elevation process..." "WARN" - Pause-Execution "About to request administrator privileges..." - - # Use the new elevation system - Start-ElevatedScript -DebugMode $Debug - - Write-Log "Elevation process completed, exiting current process" - exit - } - - Write-Log "Already running as administrator - this shouldn't happen with the new system" "WARN" - Write-Log "Proceeding with main logic anyway..." - - # If we somehow get here (running as admin), execute the main logic - # Set repo and .bat filename - $repoOwner = "Batlez" - $repoName = "Batlez-Tweaks" - $batFileName = "Batlez Tweaks.bat" - $branch = "main" - - Write-Log "Repository: $repoOwner/$repoName" - Write-Log "Branch: $branch" - Write-Log "Batch file: $batFileName" - - # Download the .bat file to temp - $tempBat = Join-Path $env:TEMP $batFileName - $rawUrl = "https://raw.githubusercontent.com/$repoOwner/$repoName/$branch/$([uri]::EscapeDataString($batFileName))" - - Write-Log "Download URL: $rawUrl" - Write-Log "Temp file location: $tempBat" - - Write-Log "Downloading batch file..." "INFO" - Invoke-WebRequest -Uri $rawUrl -OutFile $tempBat -ErrorAction Stop - Write-Log "Download completed successfully" - - # Execute the .bat file - Write-Log "Running $batFileName as administrator..." - $process = Start-Process -FilePath $tempBat -Verb RunAs -Wait -PassThru - Write-Log "Batch file execution completed with exit code: $($process.ExitCode)" - - # Cleanup - Remove-Item $tempBat -Force -ErrorAction SilentlyContinue - Write-Log "Cleanup completed" - - Write-Log "Script completed successfully!" "SUCCESS" -} -catch { - Write-Log "FATAL ERROR: $($_.Exception.Message)" "ERROR" - Write-Log "Stack trace: $($_.ScriptStackTrace)" "ERROR" - - if ($Debug) { - Write-Log "Error details:" "ERROR" - Write-Log "Exception Type: $($_.Exception.GetType().FullName)" "ERROR" - Write-Log "Inner Exception: $($_.Exception.InnerException)" "ERROR" - } -} -finally { - if ($Debug) { - Write-Log "Press any key to close this window..." - Read-Host "Press Enter to exit" - } - - # Show log file location - $logFile = Join-Path $env:TEMP "batlez-tweaks-debug.log" - if (Test-Path $logFile) { - Write-Log "Debug log saved to: $logFile" - if ($Debug) { - Write-Log "You can view the full log with: notepad `"$logFile`"" - } - } + $batchFile = Join-Path ([Environment]::GetFolderPath("MyDocuments")) "Batlez-Tweaks.bat" + $url = "https://raw.githubusercontent.com/Batlez/Batlez-Tweaks/main/Batlez%20Tweaks.bat" + + Invoke-WebRequest -Uri $url -OutFile $batchFile -ErrorAction Stop + Start-Process -FilePath $batchFile -Wait + Remove-Item $batchFile -Force -ErrorAction SilentlyContinue +} catch { + Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red }