VSBuild task fails on self-hosted Azure Pipelines Agent
Tags: azuredevopspipelines, azurepipelines, azuredevops
Published: 2023-09-21 12:43:40

---
title: VSBuild task fails on self-hosted Azure Pipelines Agent
published: true
date: 2023-09-18 13:37:29 UTC
tags: AzureDevOpsPipelines,azurepipelines,AzureDevOps,azuredevops
canonical_url: https://jessehouwing.net/vsbuild-task-fails-on-self-hosted-azure-pipelines-agent/
---

 ![VSBuild task fails on self-hosted Azure Pipelines Agent](https://jessehouwing.net/content/images/2023/09/Screenshot-2023-09-18-153051.png)

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](https://github.com/PowerShell/PowerShell/issues/18530?ref=jessehouwing.net#issuecomment-1325691850).

**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.

{% embed https://github.com/microsoft/azure-pipelines-agent/issues/4438?ref=jessehouwing.net %}

## Workaround:

Set the agent knob `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.