diff --git a/localaction.py b/localaction.py index 1b2e800..8142393 100644 --- a/localaction.py +++ b/localaction.py @@ -71,7 +71,7 @@ def run_exe(path, synchronous=False): subprocess.Popen(path, cwd=os.path.dirname(path)) except OSError as e: if "WinError 740" in str(e): - p = subprocess.Popen(["python", "adminrun.py", path], + p = subprocess.Popen(["powershell", "-ExecutionPolicy", "Bypass", "-File", "windows_run_as_admin.ps1", path], subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) print(p.communicate()) else: diff --git a/windows_run_as_admin.ps1 b/windows_run_as_admin.ps1 new file mode 100644 index 0000000..f6a7d76 --- /dev/null +++ b/windows_run_as_admin.ps1 @@ -0,0 +1,27 @@ +param ( + [string]$Path +) + +# Check if running as administrator +$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() +$Principal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser) +$IsAdmin = $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) + +if (-not $IsAdmin) { + # Relaunch the script with elevated privileges + Start-Process powershell -ArgumentList "-File `"$PSCommandPath`" `"$Path`"" -Verb RunAs + 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 } + +} catch { + Write-Host "Error running the process: $_" -ForegroundColor Red +} \ No newline at end of file