|
1 | 1 | import subprocess |
| 2 | +import sys |
2 | 3 | import tempfile |
3 | 4 | import time |
4 | 5 | from typing import Optional |
@@ -45,19 +46,27 @@ def execute_script( |
45 | 46 | script: str, scripts_args: list[str], verbose: bool, script_type: str, frid: Optional[str] = None |
46 | 47 | ) -> tuple[int, str, Optional[str]]: |
47 | 48 | temp_file_path = None |
| 49 | + script_path = file_utils.add_current_path_if_no_path(script) |
| 50 | + # On Windows, .ps1 files must be run via PowerShell, not as the executable |
| 51 | + if sys.platform == "win32" and script_path.lower().endswith(".ps1"): |
| 52 | + cmd = ["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script_path] + scripts_args |
| 53 | + else: |
| 54 | + cmd = [script_path] + scripts_args |
48 | 55 | try: |
49 | 56 | start_time = time.time() |
50 | 57 | result = subprocess.run( |
51 | | - [file_utils.add_current_path_if_no_path(script)] + scripts_args, |
| 58 | + cmd, |
52 | 59 | stdout=subprocess.PIPE, |
53 | 60 | stderr=subprocess.STDOUT, |
54 | 61 | text=True, |
| 62 | + encoding="utf-8", |
| 63 | + errors="replace", |
55 | 64 | timeout=SCRIPT_EXECUTION_TIMEOUT, |
56 | 65 | ) |
57 | 66 | elapsed_time = time.time() - start_time |
58 | 67 | # Log the info about the script execution |
59 | 68 | if verbose: |
60 | | - with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".script_output") as temp_file: |
| 69 | + with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", delete=False, suffix=".script_output") as temp_file: |
61 | 70 | temp_file.write(f"\n═════════════════════════ {script_type} Script Output ═════════════════════════\n") |
62 | 71 | temp_file.write(result.stdout) |
63 | 72 | temp_file.write("\n══════════════════════════════════════════════════════════════════════\n") |
@@ -89,7 +98,7 @@ def execute_script( |
89 | 98 | except subprocess.TimeoutExpired as e: |
90 | 99 | # Store timeout output in a temporary file |
91 | 100 | if verbose: |
92 | | - with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".script_timeout") as temp_file: |
| 101 | + with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", delete=False, suffix=".script_timeout") as temp_file: |
93 | 102 | temp_file.write(f"{script_type} script {script} timed out after {SCRIPT_EXECUTION_TIMEOUT} seconds.") |
94 | 103 | if e.stdout: |
95 | 104 | decoded_output = e.stdout.decode("utf-8") if isinstance(e.stdout, bytes) else e.stdout |
|
0 commit comments