Skip to main content

This recipe shows how to promote content between various environments.

#
# Promoting IIS Web Site Content in PowerShell
#
# Once our three-tier website scheme is set up, the next step is to configure
# the content promotion method. This recipe shows how to promote content between
# the various environments.
#
# This recipe assumes the development/staging/production website configuration
# created in the prior recipe.
#
# This will result in the promotion of the code to not occur until the very end
# of the script, but will have the added benefit of limiting the amount of time
# that the content is in an unknown state. This script also has the benefit of
# ensuring no old content is retained once the promotion occurs.
#
# This script could be separated into two components, allowing the copying of
# content to occur separately from the ultimate promotion. For instance, if the
# website is very large in size, and you have a minimal amount of time to
# promote the content, the data can be copied to the temporary location ahead of
# time. Then, during your change window, the folders are quickly renamed,
# resulting in minimal downtime of the site.
#
# Lastly, this script also has the benefit of providing a rollback method. If a
# change is made to the site and it is promoted, but then an issue is found, the
# content can be rolled back by simply renaming the folders. In the example
# shown, we could delete the wwwStage folder (because it exists in wwwDev, there
# is no need to keep the contents) and rename wwwStageOld back to wwwStage and
# the site will be the same as before the promotion.
#
# Book.......: Packt Windows Server 2012 Automation with PowerShell Cookbook
# Location...: Chapter 3: Managing IIS with PowerShell - Promoting Content in Web Sites
# Link.......: https://www.packtpub.com/networking-and-servers/windows-server-2012-automation-powershell-cookbook
#

Invoke-Command -ComputerName web1, web2 -ScriptBlock {
    Copy-Item   -Path C:\inetpub\wwwDev      -Destination C:\inetpub\wwwStageTmp -Verbose -Force -Recurse
    Rename-Item -Path C:\inetpub\wwwStage    -NewName wwwStageOld -Force -Confirm:$false
    Rename-Item -Path C:\inetpub\wwwStageTmp -NewName wwwStage -Force -Confirm:$false
}