From 445749cbda2ca3ea7bb44c7a37d5c3bd47cde957 Mon Sep 17 00:00:00 2001 From: Twangboy Date: Fri, 17 Jan 2025 14:03:07 -0700 Subject: [PATCH] Fix test_script for Windows --- salt/modules/cmdmod.py | 48 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index 72828b5e2924..4c997e40a81c 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -11,6 +11,7 @@ import glob import logging import os +import pathlib import re import shutil import subprocess @@ -2937,28 +2938,37 @@ def _cleanup_tempfile(path): os.chmod(path, 320) os.chown(path, __salt__["file.user_to_uid"](runas), -1) - if salt.utils.platform.is_windows() and shell.lower() != "powershell": - cmd_path = _cmd_quote(path, escape=False) + if salt.utils.platform.is_windows(): + if shell.lower() != "powershell": + cmd_path = _cmd_quote(path, escape=False) + else: + cmd_path = path + if not env: + env = {} + mod_paths = [ + pathlib.Path( + rf'{os.getenv("SystemRoot")}\System32\WindowsPowerShell\v1.0\Modules' + ), + pathlib.Path(rf'{os.getenv("ProgramFiles")}\WindowsPowerShell\Modules'), + pathlib.Path( + rf'{os.getenv("ProgramFiles(x86)", "")}\WindowsPowerShell\Modules' + ), + ] + ps_module_path = [ + pathlib.Path(x) for x in os.getenv("PSModulePath", "").split(";") + ] + for mod_path in mod_paths: + if mod_path.exists(): + if mod_path not in ps_module_path: + ps_module_path.append(mod_path) + mod_paths = "" + for mod_path in ps_module_path: + if mod_path: + mod_paths += f"{str(path)};" + env.update({"PSModulePath": mod_paths}) else: cmd_path = _cmd_quote(path) - if not env: - env = {} - - paths = [ - rf'{os.getenv("SystemRoot")}\System32\WindowsPowerShell\v1.0\Modules', - rf'{os.getenv("ProgramFiles")}\WindowsPowerShell\Modules', - rf'{os.getenv("ProgramFiles(x86)", "")}\WindowsPowerShell\Modules', - ] - - ps_module_path = os.getenv("PSModulePath", "").split(";") - - for path in paths: - if os.path.exists(path): - ps_module_path.append(path) - - env.update({"PSModulePath": ";".join(ps_module_path)}) - ret = _run( cmd_path + " " + str(args) if args else cmd_path, cwd=cwd,