VSBuild task fails on self-hosted Azure Pipelines Agent

VSBuild task fails on self-hosted Azure Pipelines Agent

Today I got this baffling error while trying to run one of the few pipelines I own that requires a self-hosted agent.

2023-09-18T13:24:55.1897667Z ##[section]Starting: VSBuild
2023-09-18T13:24:55.2042232Z ==============================================================================
2023-09-18T13:24:55.2042611Z Task         : Visual Studio build
2023-09-18T13:24:55.2042705Z Description  : Build with MSBuild and set the Visual Studio version property
2023-09-18T13:24:55.2042827Z Version      : 1.214.0
2023-09-18T13:24:55.2042907Z Author       : Microsoft Corporation
2023-09-18T13:24:55.2042985Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/visual-studio-build
2023-09-18T13:24:55.2043127Z ==============================================================================
2023-09-18T13:24:56.8956825Z ##[error]The following error occurred while loading the extended type data file: Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member AuditToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member AccessToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Sddl is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Access is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Group is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Owner is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Path is already present.

2023-09-18T13:24:57.0291684Z ##[error]The 'ConvertTo-SecureString' command was found in the module 'Microsoft.PowerShell.Security', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Security'.
2023-09-18T13:24:57.0552980Z ##[section]Finishing: VSBuild

I don't run this pipeline often and had re-imaged my laptop since the last run in March 2023... And was stumped by the error message. Googling a bit led me to a pretty old GitHub issue which explains the problem might happen in you run a PowerShell script in a PowerShell 5.1 terminal a Windows Command Prompt in a PowerShell Core terminal.

But I wasn't, was I?!

After some more head scratching, I figured it out. I was running my Azure Pipelines agent Interactively and had launched it from a PowerShell 7 terminal. terminating the agent, opening a Windows Command prompt, and relaunching the agent fixed things.

[BUG]: launching self-hosted agent from powershell core prompt causes powershell-based tasks to fail · Issue #4438 · microsoft/azure-pipelines-agent
What happened? I provisioned a new self-hosted agent and ran it interactively using ./run.cmd from a pwsh prompt in my system. All jobs containing a powershell based task fail with obscure error me…

Workaround:

Set the agentknob AZP_AGENT_CLEANUP_PSMODULES_IN_POWERSHELL on the agent prior to launching the agent:

$env:AZP_AGENT_CLEANUP_PSMODULES_IN_POWERSHELL="true"
.\run.cmd

You can also add this as a variable to your pipeline.