From 4785517b6c3c2b5fab0cc5ad20fd1e1580d70dea Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Sat, 15 Feb 2025 13:01:01 +0100 Subject: [PATCH] feat: implement bulk exec + output capture --- windows_run_as_admin.ps1 | 60 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/windows_run_as_admin.ps1 b/windows_run_as_admin.ps1 index f6a7d76..aad9391 100644 --- a/windows_run_as_admin.ps1 +++ b/windows_run_as_admin.ps1 @@ -1,7 +1,24 @@ param ( - [string]$Path + [string]$PathsJson ) +try { + $Paths = $PathsJson | ConvertFrom-Json -ErrorAction Stop +} catch { + # If it fails, assume it's Base64-encoded and decode it + try { + $DecodedJson = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($PathsJson)) + $Paths = $DecodedJson | ConvertFrom-Json -ErrorAction Stop + } catch { + Write-Error "Failed to parse PathsJson as JSON or Base64-encoded JSON." + exit 1 + } +} + +Write-Host "Paths" +Write-Host $PathsJson +Write-Host $PWD + # Check if running as administrator $CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() $Principal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser) @@ -9,19 +26,40 @@ $IsAdmin = $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::A if (-not $IsAdmin) { # Relaunch the script with elevated privileges - Start-Process powershell -ArgumentList "-File `"$PSCommandPath`" `"$Path`"" -Verb RunAs + $EncodedJson = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($PathsJson)) + $TempFile = New-TemporaryFile + Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -Command `"Set-Location -Path '$PWD'; & '$PSCommandPath' '$EncodedJson' *> '$TempFile'`" " -Verb RunAs -Wait + + Start-Sleep 1 + if (Test-Path $TempFile) { + # Output the captured output + Get-Content $TempFile | Write-Host + Remove-Item $TempFile + }else{ + Write-Host "No output captured. Check if the script produces any output." + } + exit } -# Run the process and capture output -try { - $Process = Start-Process -FilePath $Path -NoNewWindow -PassThru -RedirectStandardOutput output.txt -RedirectStandardError error.txt - $Process.WaitForExit() - # Read and display output - Get-Content output.txt - Get-Content error.txt | ForEach-Object { Write-Host $_ -ForegroundColor Red } +foreach($Path in $Paths){ -} catch { - Write-Host "Error running the process: $_" -ForegroundColor Red + Write-Host "Running: $Path" + + # Run the process and capture output + try { + + if ($Path -match '\.reg$') { + # Run regedit silently to merge the .reg file + $Process = Start-Process -FilePath "regedit.exe" -ArgumentList "/s `"$Path`"" -NoNewWindow -PassThru + } else { + # Run the file normally + $Process = Start-Process -FilePath $Path -NoNewWindow -PassThru + } + $Process.WaitForExit() + + } catch { + Write-Host "Error running the process: $_" + } } \ No newline at end of file