Prep cmd when shell is powershell.exe or pwsh.exe. If we were called by script(), then run it via the -File parameter, Otherwise, run the command via the -Command parameter.
(win_shell, cmd, encoded_cmd)
| 275 | |
| 276 | |
| 277 | def _prep_powershell_cmd(win_shell, cmd, encoded_cmd): |
| 278 | """ |
| 279 | Prep cmd when shell is powershell.exe or pwsh.exe. If we were called by script(), then |
| 280 | run it via the -File parameter, Otherwise, run the command via the -Command parameter. |
| 281 | """ |
| 282 | new_cmd = [ |
| 283 | win_shell, |
| 284 | "-NonInteractive", |
| 285 | "-NoProfile", |
| 286 | "-ExecutionPolicy", |
| 287 | "Bypass", |
| 288 | ] |
| 289 | |
| 290 | # extract_stack() returns a list of tuples. |
| 291 | # The last item in the list [-1] is the current method. |
| 292 | # The third item[2] in each tuple is the name of that method. |
| 293 | stack = traceback.extract_stack(limit=3) |
| 294 | if stack[-3][2] == "script": |
| 295 | new_cmd.append("-File") |
| 296 | new_cmd.extend(cmd) |
| 297 | elif encoded_cmd: |
| 298 | new_cmd.extend(["-EncodedCommand", cmd]) |
| 299 | else: |
| 300 | new_cmd.append("-Command") |
| 301 | if isinstance(cmd, list): |
| 302 | cmd = " ".join(cmd) |
| 303 | new_cmd.append(cmd) |
| 304 | |
| 305 | log.debug(new_cmd) |
| 306 | return new_cmd |
| 307 | |
| 308 | |
| 309 | def _run( |