Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display warning when the agent is run in PowerShell Core #4778

Merged
merged 39 commits into from
Jun 12, 2024

Conversation

aleksandrlevochkin
Copy link
Contributor

@aleksandrlevochkin aleksandrlevochkin commented Apr 30, 2024

WI

AB#2142810

Problem

The problem typically arises when the agent is started from PowerShell Core and executes a PowerShell task (doesn't matter if the pwsh flag is set or not). In this case the environment variable PSModulePath is copied from PowerShell Core to the agent to Windows PowerShell. Thus, Windows PowerShell attempts to use modules from PowerShell Core locations. This is described in more details here: PowerShell/PowerShell#18530 (comment)

Solution

Added a new knob AZP_AGENT_CHECK_PSMODULES_LOCATIONS. When it is enabled, when the agent is started in PowerShell Core the following message is displayed in the terminal:

image

The same message is output to the pipeline if it attempts to execute a Windows PowerShell task and the PSModulePath env variable contains some well-known PowerShell Core locations:

image

Also made some changes to prevent memory problems related to finding parent processes:

  • Added a max search depth for the parent process (set to 10)
  • The list of processes is now disposed right after doing the necessary checks
  • Removed AZP_AGENT_USE_INTEROP_TO_FIND_PARENT_PROCESS knob and related code

Testing

Tested with several Powershell scripts in my test org.
Added L0 tests.

@aleksandrlevochkin aleksandrlevochkin marked this pull request as ready for review May 8, 2024 12:59
@aleksandrlevochkin aleksandrlevochkin requested review from a team as code owners May 8, 2024 12:59
Copy link

azure-boards bot commented May 8, 2024

✅ Successfully linked to Azure Boards work item(s):

@aleksandrlevochkin aleksandrlevochkin changed the title Fix issue with PSModulePath Add knobs for modifying PSModulePath and checking if the agent is run from Powershell once per job May 8, 2024
Copy link
Contributor

@merlynomsft merlynomsft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some feedback separately; let's discuss

@aleksandrlevochkin aleksandrlevochkin changed the title Add knobs for modifying PSModulePath and checking if the agent is run from Powershell once per job Display warning when the agent is run in PowerShell Core May 16, 2024
@kirill-ivlev kirill-ivlev enabled auto-merge (squash) May 31, 2024 12:06
@kirill-ivlev kirill-ivlev merged commit 8924d57 into master Jun 12, 2024
12 checks passed
@timbrigham-oc
Copy link

@aleksandrlevochkin, @KonstantinTyukalov @kirill-ivlev, @merlynomsft this request is the first time in a long time I've seen an update to to PowerShell3Handler.cs.

Can you kindly take a look at my pull request #4502 which also touches that file? There's a long standing bug there that I would love to see addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants