Skip to content

Instantly share code, notes, and snippets.

@EtherZa
Created November 6, 2017 06:45
Show Gist options
  • Save EtherZa/8b10d7b68eb1e9bda616297ec54a836b to your computer and use it in GitHub Desktop.
Save EtherZa/8b10d7b68eb1e9bda616297ec54a836b to your computer and use it in GitHub Desktop.
Synchronise IIS hosted website across multiple servers via web deploy. Console output for TeamCity support.
# Parameters are expected in the following order:
# 1. Path to ms deploy eg. "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
# 2. Source server name eg. server1.domain.net
# 3. Path to setParameters.xml which contains 'IIS Web Application Name' override [Comma delimitted]
# 4. User name eg. domain\username
# 5. Password eg. ****** (Caveat: not escaped)
# 6. Destination server name(s) [Comma delimitted] eg. server2.domain.net,server3.domain.net
param (
[parameter(Mandatory=$true)] [string]$msdeploy,
[parameter(Mandatory=$true)] [string]$source,
[parameter(Mandatory=$true)] [string]$config,
[parameter(Mandatory=$true)] [string]$username,
[parameter(Mandatory=$true)] [string]$password,
[parameter(Mandatory=$true)] [string]$destination
)
try
{
Write-Host "##teamcity[blockOpened name='Parameters']"
Write-Host "msdeploy => '$msdeploy'"
Write-Host "source => '$source'"
Write-Host "config => '$config'"
Write-Host "destination => '$destination'"
Write-Host "##teamcity[blockClosed name='Parameters']"
$configs = $config.Split(",")
foreach ($config in $configs)
{
$file = New-Object System.IO.FileInfo ($config)
if (!$file.Exists)
{
throw "'$config' not found (Working: '" + (Get-Location) + "')."
}
[xml]$xml = Get-Content $file.FullName
$app = $xml.selectSingleNode("//setParameter[@name='IIS Web Application Name']/@value").Value
if ([string]::IsNullOrEmpty($app))
{
Write-Host $("##teamcity[message text='{0} does not specify IIS Web Application Name']" -f $file)
continue
}
$destination.Split(",") | ForEach {
$server = $_
$name = $("Syncing: [{0}|] {1} => {2}" -f $app, $source, $server)
Write-Host $("##teamcity[blockOpened name='{0}']" -f $name)
$command = $("`"{0}`" -verb:sync -source:iisApp='{1}',computername={2},userName='{3}',password='{4}',includeAcls='True',AuthType='NTLM' -dest:iisApp='{1}',computername='{5}',username='{3}',password='{4}',includeAcls='True',AuthType='NTLM'" -f $msdeploy, $app, $source, $username, $password, $server)
cmd.exe /C "`"$command`""
if ($LastExitCode -ne 0) {
[System.Environment]::Exit(1)
}
Write-Host $("##teamcity[blockClosed name='{0}']" -f $name)
}
}
} Catch {
Write-Host $("##teamcity[message text='{0}' status='ERROR']" -f $_.Exception.Message.Replace("]", "|]"))
[System.Environment]::Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment