From 2fc5af765b7882cf3260a5e02ddc4ff1ca3c6b1a Mon Sep 17 00:00:00 2001 From: Twangboy Date: Fri, 12 May 2023 12:55:21 -0600 Subject: [PATCH] Add ability to reprovision an app --- salt/modules/win_appx.py | 31 +++++++++++++++++++++++++------ salt/utils/win_pwsh.py | 4 +++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/salt/modules/win_appx.py b/salt/modules/win_appx.py index 3edcded207f..99ef17078a7 100644 --- a/salt/modules/win_appx.py +++ b/salt/modules/win_appx.py @@ -5,6 +5,8 @@ import salt.utils.win_reg log = logging.getLogger(__name__) +CURRENTVERSION_KEY = r"SOFTWARE\Microsoft\Windows\CurrentVersion" +DEPROVISIONED_KEY = fr"{CURRENTVERSION_KEY}\Appx\AppxAllUserStore\Deprovisioned" __virtualname__ = "appx" @@ -56,7 +58,7 @@ def get(query=None, field="Name", include_store=False, frameworks=False, bundles return _pkg_list(__utils__["win_pwsh.run_dict"](" | ".join(cmd)), field) -def remove(query=None, include_store=False, frameworks=False, bundles=True): +def remove(query=None, include_store=False, frameworks=False, bundles=True, deprovision=False): packages = get( query=query, field=None, @@ -66,7 +68,6 @@ def remove(query=None, include_store=False, frameworks=False, bundles=True): ) def remove_package(package): - remove_name = package["PackageFullName"] # If the package is part of a bundle with the same name, removal will # fail. Let's make sure it's a bundle @@ -80,12 +81,16 @@ def remove(query=None, include_store=False, frameworks=False, bundles=True): bundles=True, ) if bundle and bundle["IsBundle"]: + log.debug(f'Found bundle: {bundle["PackageFullName"]}') remove_name = bundle["PackageFullName"] - log.debug("Removing package: %s", remove_name) - __utils__["win_pwsh.run_dict"]( - f"Remove-AppxPackage -AllUsers -Package {remove_name}" - ) + if deprovision: + log.debug("Deprovisioning package: %s", remove_name) + remove_cmd = f"Remove-AppxProvisionedPackage -Online -PackageName {remove_name}" + else: + log.debug("Removing package: %s", remove_name) + remove_cmd = f"Remove-AppxPackage -AllUsers -Package {remove_name}" + __utils__["win_pwsh.run_dict"](remove_cmd) if isinstance(packages, list): log.debug("Removing %s packages", len(packages)) @@ -99,3 +104,17 @@ def remove(query=None, include_store=False, frameworks=False, bundles=True): return None return True + + +def get_deprovisioned(): + return salt.utils.win_reg.list_keys(hive="HKLM", key=f"{DEPROVISIONED_KEY}") + + +def reprovision(package_name): + key = f"{DEPROVISIONED_KEY}\\{package_name}" + if salt.utils.win_reg.key_exists(hive="HKLM", key=key): + log.debug(f"Deprovisioned app found: {package_name}") + ret = salt.utils.win_reg.delete_key_recursive(hive="HKLM", key=key) + return not ret["Failed"] + log.debug(f"Deprovisioned app not found: {package_name}") + return None diff --git a/salt/utils/win_pwsh.py b/salt/utils/win_pwsh.py index 1718e1053eb..53d07322e47 100644 --- a/salt/utils/win_pwsh.py +++ b/salt/utils/win_pwsh.py @@ -40,7 +40,9 @@ def run_dict(cmd, cwd=None): successfully """ if "convertto-json" not in cmd.lower(): - cmd = "{} | ConvertTo-Json".format(cmd) + cmd = f"{cmd} | ConvertTo-Json" + if "progresspreference" not in cmd.lower(): + cmd = f"$ProgressPreference = 'SilentlyContinue'; {cmd}" log.debug("PowerShell: %s", cmd) ret = __salt__["cmd.run_all"](cmd, shell="powershell", cwd=cwd)