From 797a645f93a315978590e24cbb6bbca0c4d706ae Mon Sep 17 00:00:00 2001 From: Stefan Markovic Date: Fri, 29 Dec 2023 12:22:19 +0100 Subject: [PATCH] If GPO is set, enable/disable module on PT start depending on gpo value --- .../CmdNotFoundModuleInterface.vcxproj | 2 + ...CmdNotFoundModuleInterface.vcxproj.filters | 11 +++++- .../CmdNotFoundModuleInterface/dllmain.cpp | 37 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj index e89026c55e..a7fd427c2a 100644 --- a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj +++ b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj @@ -46,6 +46,7 @@ Windows true false + Shlwapi.lib;$(CoreLibraryDependencies);%(AdditionalDependencies) @@ -64,6 +65,7 @@ true true false + Shlwapi.lib;$(CoreLibraryDependencies);%(AdditionalDependencies) diff --git a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj.filters b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj.filters index 4a11886b7c..1834d3ae88 100644 --- a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj.filters +++ b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj.filters @@ -1,5 +1,6 @@  - + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} @@ -21,6 +22,9 @@ Header Files + + Header Files + @@ -33,4 +37,9 @@ Source Files + + + Resource Files + + \ No newline at end of file diff --git a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp index bb54ef1b62..88ddda7fd5 100644 --- a/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp +++ b/src/modules/cmdNotFound/CmdNotFoundModuleInterface/dllmain.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -45,6 +46,30 @@ class CmdNotFound : public PowertoyModuleIface private: bool m_enabled = false; + void install_module() + { + auto module_path = get_module_folderpath(); + + std::string command = "pwsh.exe"; + command += " "; + command += "-NoProfile -NonInteractive -NoLogo -WindowStyle Hidden -ExecutionPolicy Unrestricted -File \"" + winrt::to_string(module_path) + "\\WinUI3Apps\\Assets\\Settings\\Scripts\\EnableModule.ps1" + "\"" + " -scriptPath \"" + winrt::to_string(module_path) + "\""; + + system(command.c_str()); + Trace::EnableCmdNotFoundGpo(true); + } + + void uninstall_module() + { + auto module_path = get_module_folderpath(); + + std::string command = "pwsh.exe"; + command += " "; + command += "-NoProfile -NonInteractive -NoLogo -WindowStyle Hidden -ExecutionPolicy Unrestricted -File \"" + winrt::to_string(module_path) + "\\WinUI3Apps\\Assets\\Settings\\Scripts\\DisableModule.ps1" + "\""; + + system(command.c_str()); + Trace::EnableCmdNotFoundGpo(false); + } + public: CmdNotFound() { @@ -55,6 +80,18 @@ public: logFilePath.append(LogSettings::cmdNotFoundLogPath); Logger::init(LogSettings::cmdNotFoundLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location()); Logger::info("CmdNotFound object is constructing"); + + powertoys_gpo::gpo_rule_configured_t gpo_rule_configured_value = gpo_policy_enabled_configuration(); + if (gpo_rule_configured_value == powertoys_gpo::gpo_rule_configured_t::gpo_rule_configured_enabled) + { + install_module(); + m_enabled = true; + } + else if (gpo_rule_configured_value == powertoys_gpo::gpo_rule_configured_t::gpo_rule_configured_disabled) + { + uninstall_module(); + m_enabled = false; + } } virtual powertoys_gpo::gpo_rule_configured_t gpo_policy_enabled_configuration() override