Michael Sagalovich
Michael Sagalovich

Reputation: 2549

Schedule a task after deployment

I need to setup a scheduled task in Windows Task Scheduler (v2.0 on Windows Server 2008 R2) right after my web site deploys.

I am using TFS 2010 to build my application, and apparently my MSBuild Arguments contain arguments /P:DeployOnBuild=True /P:DeployTarget=MSDeployPublish /P:CreatePackageOnPublish=true /P:MSDeployPublishMethod=WMSvc.

I know that I can use Schtasks.exe to setup a scheduled task via command line, I also know there is a runCommand provider for MsDeploy. So I thought I could use runCommand to run Schtasks.exe with required parameters.

My question is how I do it in TFS and MsBuild. I assumed I could pass some parameters to MsBuild, and they would be transferred "as is" to MsDeploy, but I could not find how I do it.

Upvotes: 3

Views: 1829

Answers (1)

Richard Szalay
Richard Szalay

Reputation: 84754

If it must be after the build, you'll need to use the postSync msdeploy argument to execute a runCommand. Since postSync is not available from Visual Studio's MSBuild tasks, you'll need to generate a package and then run the generated cmd file with the postSync argument tacked onto the end.

Package.cmd -postSync:runCommand="c:\windows\system32\schtasks.exe arguments"

If it should be after the build, you can include an additional provide by adding the following to your publish profile (pubxml), .wpp.targets file or your project file:

<ItemGroup>
  <MsDeploySourceManifest Include="runCommand">
    <Path>&quot;c:\Windows\system32\schtasks.exe&quot; &quot;Arguments here&quot;</Path>
  </MsDeploySourceManifest>
</ItemGroup>

It's not officially guarantees that providers run in order, but in practise they do. You might need to hook a target into the right event, though, so you can register your runCommand after the other providers.

Upvotes: 1

Related Questions