From 3517c9f080bc716324c3100b4b5a4646a5ad627f Mon Sep 17 00:00:00 2001 From: Denis Rumyantsev Date: Tue, 18 Jul 2023 19:14:48 +0200 Subject: [PATCH] Add `AZP_AGENT_CLEANUP_PSMODULES_IN_POWERSHELL` agent knob (#4360) * add knob agent in pwsh * rename knob --- src/Agent.Sdk/Knob/AgentKnobs.cs | 7 +++++++ src/Agent.Worker/Handlers/Handler.cs | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Agent.Sdk/Knob/AgentKnobs.cs b/src/Agent.Sdk/Knob/AgentKnobs.cs index 6517bf1540..0414f63b2e 100644 --- a/src/Agent.Sdk/Knob/AgentKnobs.cs +++ b/src/Agent.Sdk/Knob/AgentKnobs.cs @@ -486,5 +486,12 @@ public class AgentKnobs new RuntimeKnobSource("AGENT_FORCE_CREATE_TASKS_DIRECTORY"), new EnvironmentKnobSource("AGENT_FORCE_CREATE_TASKS_DIRECTORY"), new BuiltInDefaultKnobSource("false")); + + public static readonly Knob CleanupPSModules = new Knob( + nameof(CleanupPSModules), + "Removes the PSModulePath environment variable if the agent is running in PowerShell.", + new RuntimeKnobSource("AZP_AGENT_CLEANUP_PSMODULES_IN_POWERSHELL"), + new EnvironmentKnobSource("AZP_AGENT_CLEANUP_PSMODULES_IN_POWERSHELL"), + new BuiltInDefaultKnobSource("false")); } } diff --git a/src/Agent.Worker/Handlers/Handler.cs b/src/Agent.Worker/Handlers/Handler.cs index 6c70872cff..2e4306e795 100644 --- a/src/Agent.Worker/Handlers/Handler.cs +++ b/src/Agent.Worker/Handlers/Handler.cs @@ -300,7 +300,8 @@ protected void AddPrependPathToEnvironment() protected void RemovePSModulePathFromEnvironment() { - if (PlatformUtil.RunningOnWindows && WindowsProcessUtil.AgentIsRunningInPowerShell()) + if (AgentKnobs.CleanupPSModules.GetValue(ExecutionContext).AsBoolean() && + PlatformUtil.RunningOnWindows && WindowsProcessUtil.AgentIsRunningInPowerShell()) { AddEnvironmentVariable("PSModulePath", ""); Trace.Info("PSModulePath removed from environment since agent is running on Windows and in PowerShell.");