title: Failed to add VM resource to Azure DevOps Environment
body: <p>I followed the <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/environments-virtual-machines?view=azure-devops" rel="nofollow noreferrer">steps</a> to add a VM resource to an environment but got an error when running the script. I searched the error message but couldn't find anything.</p>
  <p>Error messaege: <code>Failed to add virtual machine resource. Linked environment pool is null.</code></p>
  <p>The log file has these:</p>
  <pre><code>[2021-10-18 23:03:25Z ERR  VisualStudioServices] POST request to https://dev.azure.com/xxx/guid/_apis/pipelines/environments/3/providers/virtualmachines failed. HTTP Status: InternalServerError, AFD Ref: Ref A: guid Ref B: WSTEDGE0610 Ref C: 2021-10-18T23:03:25Z
  [2021-10-18 23:03:25Z INFO CommandSettings] Flag 'unattended': 'False'
  [2021-10-18 23:03:25Z ERR  Terminal] WRITE ERROR (exception):
  [2021-10-18 23:03:25Z ERR  Terminal] Microsoft.TeamFoundation.DistributedTask.WebApi.VirtualMachineResourceLinkedPoolNullException: Failed to add virtual machine resource. Linked environment pool is null. 
     at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.HandleResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken)
     at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.SendAsync(HttpRequestMessage message, HttpCompletionOption completionOption, Object userState, CancellationToken cancellationToken)
     at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.SendAsync[T](HttpRequestMessage message, Object userState, CancellationToken cancellationToken)
     at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.SendAsync[T](HttpMethod method, IEnumerable`1 additionalHeaders, Guid locationId, Object routeValues, ApiResourceVersion version, HttpContent content, IEnumerable`1 queryParameters, Object userState, CancellationToken cancellationToken)
     at Microsoft.VisualStudio.Services.Agent.Listener.Configuration.EnvironmentVMResourceConfigProvider.AddAgentAsync(AgentSettings agentSettings, TaskAgent agent, CommandSettings command)
     at Microsoft.VisualStudio.Services.Agent.Listener.Configuration.ConfigurationManager.ConfigureAsync(CommandSettings command)
  [2021-10-18 23:03:25Z ERR  Terminal] WRITE ERROR: Failed to add the agent.  Try again or ctrl-c to quit
  </code></pre>
  <p>The config command is basically the following:</p>
  <p><code>config --environment --environmentname DEV --agent computername --runasservice --work _work --url https://dev.azure.com/xxx/ --projectname myprojectname --auth PAT --token xxx</code></p>
  <p>I am the org admin on DevOps and ran the script as server admin on the on-prem VM.</p>
  <p><strong>Update</strong></p>
  <p>After using the <code>--unattanded</code> option:</p>
  <p><code>config --unattanded --disableloguploads --environment --environmentname DEV --pool org-agent-pool-nm --agent computername --runasservice --windowsLogonAccount xxx --windowsLogonPassword xxx --work _work --url https://dev.azure.com/xxx/ --projectname myprojectname --auth PAT --token xxx</code></p>
  <p>I got a different error.</p>
  <pre><code>Flag 'unattended': 'True' 
  Failed to add virtual machine resource. Linked environment pool is null. 
  </code></pre>
  <p><strong>Update 2</strong></p>
  <p>I found others are having the same or similar issues.</p>
  <p><a href="https://stackoverflow.com/questions/61955974/azure-devops-pipeline-environment-resource-agent-installation-issue">Azure Devops Pipeline Environment Resource Agent installation issue</a></p>
  <p><a href="https://developercommunity.visualstudio.com/t/addition-of-resource-to-environment-fails-for-user/1048111" rel="nofollow noreferrer">https://developercommunity.visualstudio.com/t/addition-of-resource-to-environment-fails-for-user/1048111</a></p>