user1869558 user1869558 - 1 year ago 109
PowerShell Question

How to get a list of TFS build agents via API/Powershell?

I'm building a tool for checking disk space on the TFS Build servers. I could just input a manual list but for longterm use, it's best if I could import the list of build servers from somewhere. I've connected to TFS with the following code

add-type -Path "C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\Microsoft.TeamFoundation.Build.Workflow.dll"
$TFSURL = "http://tfs:8080/tfs/"
$TFS = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSURL)

$buildServer = $TFS.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

Not sure how to proceed from here. I haven't been able to find much relevant documentation or my googling skills are rusty.

Answer Source

So you're looking for the Agents, and to get those you need to get the controllers that they're associated with, so you need to run the QueryBuildControllers() method of your IBuindServer object.

$buildControllers = $buildServer.QueryBuildControllers()

I found this by doing a search for Client.IBuildServer on Google, and going to the first link to MSDN. From there I just looked for a method that would find all of something that looked relevant to servers, and saw the QueryBuildControllers method, followed it to see the return objects had a property named Agents, which should be what you're looking for.