Executing PowerShell Scripts on Remote Machines with TFS 2010 and Team Deploy 2010

Image

March 1, 2023

Team Deploy 2010 is a custom add-on for Team Foundation Server 2010 (TFS) to deploy MSIs to servers and PCs. The deploy activity uses an XML file to manage the servers and steps for deployment including starting/stopping services and installing/uninstalling the MSIs. This is very effective for automated deployments in environments where automated deployments are allowed. This however does not provide a practice run into downstream environments where automated deployments are not allowed such as Staging/Integration and Production.

An alternative to this that does offer some deployment consistencies beyond the MSI is to have Team Deploy 2010 (or Team Deploy for TFS 2008 also supports this) execute a PowerShell script to perform the deployment steps. The advantage of this is that the PowerShell scripts can also be used to perform the manual deployments to these other environments. This won’t work in every scenario but should in a lot. In this post, I am going to explain how to do this.

The first thing to do is to install Team Deploy 2010. This is free and can be downloaded at http://TeamDeploy.CodePlex.com. The installation instructions are detailed on the site. For this, I will assume Team Deploy 2010 is already installed.

Next open Visual Studio 2010 and create a new build definition workflow. Create 3 arguments called RemoteExecuteFilename, TargetMachine, and RemoteCommand.

Instead of using the Deploy activity, add the RemoteExecute activity in an AgentScope container.

image_thumb_26.png

Set the properties on the RemoteExecute activity to the arguments passed in.

image_thumb_27.png

Next, set the properties that were exposed as arguments of the build definition. For the RemoteCommand, here is where you want to specify calling PowerShell.exe and the script file that will be executed on the target machine. One thing I have learned after taking this snapshot is that if you have a space in the path for the script than use this syntax:

PowerShell.exe –File “\\buildserver\deploy scripts\Update.ps1”

Next specify the path where the PSTools were installed and finally specify the machine that you want to run the remote script.

image_thumb_28.png

The final step is to create the deployment script. Thanks to the power of PowerShell, these deployment scripts can perform any action. I have created steps for starting/stopping services, applying SQL Server schema changes, search and replace strings in configuration files, etc. Essentially anything you can do in a batch file and in .NET code, can be done in PowerShell.

Here is a small sample script that I created. I have creates some much more complicated scripts and ran them on remote machines without any issues.

"Performing removal steps..."

$servicename = "PLA"
$service = Get-Service $servicename
if($service.Status -eq "Running")
{
   "Stopping " + $servicename
   Stop-Service $servicename
}
"status=" + $service.Status
Remove-Item "c:\miketest2"
msiexec /qn /x "{26260DBA-1519-4967-9118-D827793EF3B3}"

"Removal complete. Starting the installation steps..."

msiexec /qb! /i "\\buildserver\deploy\simple.msi"
New-Item "c:\miketest2" -type directory

"Applying SQL Server Schema changes..."
sqlcmd -S W2K8R2BOOT -E -i \\buildserver\deploy\dropaddcooltable.sql

if($service.Status -eq "Stopped")
{
   "Starting " + $servicename
   Start-Service $servicename
}

This is it. Here are also a couple things to consider. Copy MSIs, SQL Scripts, and the deployment script to a versioned folder. The folder is the snapshot in time including the deployment file. Keep the deployment scripts in source control. Lastly there is a new feature in PowerShell 2.0 called PowerShell Remoting. I have tried it, but it looks like this could also work. It is on my list to research and I will be sure to report back when I find out more information.

Enjoy!

Mike