Synchronizing files and executing commands on a remote server using MSDeploy

Microsoft WebDeploy (MSDeploy) is a very useful tool, not only for deploying web sites. In this post I will show you how MSDeploy can be used as a tool for synchronizing local and remote folders and executing commands on a remote server.

The first thing you need to do is to install WebDeploy. It can be installed using Web Platform Installer or by downloading the .msi file. I prefer to download the .msi. It can be found here. Download the installer and run this (you can change the port and the address to whatever you want):

msiexec /I WebDeploy_x64_en-US.msi /passive ADDLOCAL=ALL LISTENURL=http://+:80/MsDeployAgentService/

Then start the service:

net start msdepsvc

To check if the service is up running execute:

c:\program files\iis\microsoft web deploy\msdeploy.exe -verb:sync
userName=remoteUser,password=remotePassword -verbose

If the service is up running a lot of information will be printed to the screen, if not, MSDeploy will complain on that the remote service did not respond with “v1” of MSDeploy.

The sync script

Bellow is a simple PowerShell script that has two functions.

Send-Files – Synchronizes a local folder to a remote server.

Execute-RemoteCommand – Executes a batch file on a remote server. The file must already be in place on the server.

$MSDeployExe = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
$RemoteHost = "http://localhost:80/MsDeployAgentService"
$Credentials = ""
$LocalDir = "C:\temp\LocalDir"
$RemoteDir = "C:\temp\RemoteDir"

function Send-Files {
	param (

	Write-Host "Sending files to $WebDeployService`: $RemoteDir" -ForegroundColor Yellow

	if ($Credentials -ne "") {
		$Credentials = ",getCredentials=" + $Credentials

	& $MSDeployExe "-verb:sync" "-source:dirPath=$LocalDir" "-dest:dirPath=$RemoteDir,computername=$WebDeployService$Credentials" "-verbose"
	$successful = $?
	if (-not $successful) {
		throw "Failed sending files"

function Execute-RemoteCommand {
	param (
		[int]$waitInterval = 15000
	$command = Join-Path $RemoteDir $BatchFile
	Write-Host "Executing $command on $WebDeployService" -ForegroundColor Yellow
	& $MSDeployExe "-verb:sync" "-source:runCommand='$command',waitInterval=$waitInterval,waitAttempts=1" "-dest:auto,computername=$RemoteHost$Credentials" "-verbose"
	$successful = $?
	if (-not $successful) {
		throw "Failed executing command"

# Test 
Send-Files $RemoteHost $LocalDir $RemoteDir $Credentials
Execute-RemoteCommand $RemoteHost $RemoteDir "HelloWorld.bat" $Credentials


Leave a Reply

Your email address will not be published. Required fields are marked *