Remote Controlling Windows Services in PowerShell

Remote Desktop-ing into several machines just to enable or disable a service is a bit of a chore. PowerShell saves us from this tedium by providing a graceful solution via remoting. In this instance I am playing around with TFS build machines, trying to turn a few "pets" into repeatable "cattle". Sometimes I want to experiment with a build server as a live build agent and most of the time I don't want it to appear in the build agent pool. Whether a machine is available to take builds from TFS is controlled by the status of the build agent service.


# Stop Tfs agent on all machines
$Computers = @( 'COMPUTER-1', 'COMPUTER-2', 'COMPUTER-3' )
$VSOAgent = 'vsoagent.tgtfs*' 

$Computers | % { Get-Service  -ComputerName $_ -Name $VSOAgent | Set-Service -Status Stopped }

I use -Status Stopped to stop each agent and -Status Running to restart.