(
command: Union[str, Sequence[str]],
*,
monitor_output: bool = False,
termination_str: Optional[str] = None,
cancel_event: Optional[threading.Event] = None,
allow_continue_on_fail: bool = False,
)
| 122 | |
| 123 | |
| 124 | def run_powershell_command( |
| 125 | command: Union[str, Sequence[str]], |
| 126 | *, |
| 127 | monitor_output: bool = False, |
| 128 | termination_str: Optional[str] = None, |
| 129 | cancel_event: Optional[threading.Event] = None, |
| 130 | allow_continue_on_fail: bool = False, |
| 131 | ) -> int: |
| 132 | if not isinstance(command, str): |
| 133 | command = "".join(command) |
| 134 | cmd = [ |
| 135 | "powershell.exe", |
| 136 | "-NoProfile", |
| 137 | "-ExecutionPolicy", |
| 138 | "Bypass", |
| 139 | "-Command", |
| 140 | command, |
| 141 | ] |
| 142 | logger.info(f"Launching PowerShell command: {command}") |
| 143 | creationflags = 0 |
| 144 | try: |
| 145 | proc = subprocess.Popen( |
| 146 | cmd, |
| 147 | stdout=subprocess.PIPE, |
| 148 | stderr=subprocess.PIPE, |
| 149 | text=True, |
| 150 | encoding="utf-8", |
| 151 | errors="replace", |
| 152 | bufsize=1, |
| 153 | creationflags=creationflags, |
| 154 | ) |
| 155 | except Exception as e: |
| 156 | logger.exception(f"Failed to start PowerShell command: {e}") |
| 157 | show_error_popup( |
| 158 | t("errors.powershell_launch_failed", {"error": e}), |
| 159 | allow_continue=allow_continue_on_fail, |
| 160 | ) |
| 161 | raise |
| 162 | termination_detected = False |
| 163 | |
| 164 | |
| 165 | |
| 166 | def _stream(pipe, log_fn, label): |
| 167 | nonlocal termination_detected |
| 168 | for line in iter(pipe.readline, ""): |
| 169 | text = line.rstrip() |
| 170 | log_fn(f"PCOMMAND {label}: {text}") |
| 171 | if monitor_output and termination_str and termination_str in text: |
| 172 | logger.info(f"Termination string '{termination_str}' detected.") |
| 173 | termination_detected = True |
| 174 | try: |
| 175 | proc.terminate() |
| 176 | except Exception: |
| 177 | pass |
| 178 | break |
| 179 | pipe.close() |
| 180 | threads = [] |
| 181 | for pipe, fn, lbl in ( |
no test coverage detected