The Deploy test agent task in TFS release definition fails with the below exception:
Unhandled Exception: System.Management.Automation.PSInvalidOperationException: The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support Services. 2017-06-16T08:49:49.9634068Z
at System.Management.Automation.MshCommandRuntime.ThrowIfWriteNotPermitted(Boolean needsToWriteToPipeline) 2017-06-16T08:49:49.9634068Z at System.Management.Automation.MshCommandRuntime.WriteHelper_ShouldWrite(ActionPreference preference, ContinueStatus lastContinueStatus) 2017-06-16T08:49:49.9634068Z at System.Management.Automation.MshCommandRuntime.WriteWarning(WarningRecord record, Boolean overrideInquire) 2017-06-16T08:49:49.9634068Z at System.Management.Automation.Cmdlet.WriteWarning(String text) 2017-06-16T08:49:49.9634068Z at Microsoft.TeamFoundation.DistributedTask.Task.DistributedTestAutomation.Logger.WriteWarning(String message) 2017-06-16T08:49:49.9634068Z at Microsoft.TeamFoundation.DistributedTask.Task.DistributedTestAutomation.TestAgentDownloader.wc_DownloadFileCompleted(Object sender, AsyncCompletedEventArgs e) 2017-06-16T08:49:49.9634068Z at System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e) 2017-06-16T08:49:49.9634068Z at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 2017-06-16T08:49:49.9634068Z at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 2017-06-16T08:49:49.9634068Z at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 2017-06-16T08:49:49.9634068Z at System.Threading.ThreadPoolWorkQueue.Dispatch() 2017-06-16T08:49:55.4477257Z ##[error]PowerShell script completed with 255 errors.
The task executes successfully if the Update test agent check box is not checked. Solution mentioned here https://github.com/Microsoft/vsts-tasks/issues/2964 and https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dae4c62d-cd23-40ad-83cf-2555d37c0bb9/test-agent-deployment-failing?forum=tfsbuild does not seems to work either