Have you ever wondered why the box to change the name or “Alias” on an application or virtual directory is greyed out (see screenshot below)? I found a way to change the name without recreating all your settings. It uses the built in administration commands in IIS7, called appcmd.

We use Web Deployment Projects which automates much of the build process, such as minifying CSS and JavaScript and copying new files to the web servers. But we still had to deal with the pain of manually controlling IIS after a publish. This actually required remoting into the servers, and changing the home directories of the site in IIS.

This was always a small headache, but if we only published once a month or so, it wasn’t a big deal. A few months ago, we started doing weekly releases in Foliotek, our assessment portfolio tool to take care of any problems that customers were having on the live site.

Here is what we used to do:

Build the WebDeploy project in ‘release’ mode. This will copy files to a StagingWeb folder on both servers.

Load up the staging website (which is hosted on Server1 and pointing directly at the StagingWeb folder). Verify any fixes.

(Here is the annoying part): Remote into both servers, and open IIS Manager. At approximately the same time, switch the home folder over to StagingWeb and make sure the site is still up. Back up the Web folder, delete it and copy StagingWeb then rename the copy to Web. Point IIS back to the Web folder.

We have extra steps in part 3 to prevent any downtime, but it is tedius and prone to error, timing problems,etc. And it is not something to look forward to at the start of the week. Why should we do something manually that should be easy to automate?

Here is what we do now:

Build the WebDeploy project in ‘release’ mode. This will copy files to a StagingWeb folder on both servers.

Load up the staging website (which is hosted on Server1 and pointing directly at the StagingWeb folder). Verify any fixes.

It is just a couple of batch scripts. You can call them on the command line, or through MSBuild. We actually have a Windows Forms project that gets run on the AfterBuild event of our Web Deployment project. This executable has links to all of the sites, shortcuts to remote desktop, status of the servers, and a link to the publish script.

The source code is below. It would probably need to be modified for different environments, and I’m sure there are better ways to handle this problem, at least this is simple to read and modify.

publish_server_connection.bat

This file basically just calls PSExec on the given server passing up the myproject_publish.bat script.

@ECHO off
SETLOCAL
REM Publish Connection Script
REM This will call the publish.bat script on the server specified in the first argument
REM Usage: publish_server_connection.bat [servername]
SET server="%1"
SET server_dir="%1c$inetpubwwwrootMyProject"
SET psexec="%~dp0psexec.exe"
SET publish="%~dp0myproject_publish.bat"
SET usage=USAGE: publish_server_connection.bat [servername]
if not exist %server_dir% (
ECHO ERROR: The path %server_dir% does not exist! Exiting..
ECHO %usage%
GOTO End
)
if not exist %psexec% (
echo ERROR: Could Not Find PSEXEC at path: %psexec%
GOTO End
)
if not exist %publish% (
echo ERROR: Could Not Find PUBLISH at path: %publish%
GOTO End
)
ECHO Starting publish on %server%.
%psexec% %server% -c -v %publish%
if ErrorLevel 1 (
ECHO.
ECHO ERROR: Having problems starting PSExec. Please verify access to the server, and retry.
ECHO If the problem persists, then you will need to manually publish.
ECHO.
)
:End
ENDLOCAL

myproject_publish.bat

This file will be copied to the servers using PSExec, and will be executed locally in the server environment.

@ECHO off
SETLOCAL
REM Publish Script
REM This is called from development machines, using the PSExec Command.
CLS
ECHO.
ECHO =======================================================
ECHO Build Script.
ECHO %COMPUTERNAME%
ECHO This script will:
ECHO 1. Point the IIS entry for the LIVE website to StagingWeb
ECHO 2. Backup the original Web folder
ECHO 3. Copy StagingWeb over the original Web folder
ECHO 4. Point the IIS entry to the new Web folder
ECHO =======================================================
Echo.
ECHO Make sure you go to the staging site and confirm it is ready to go live.
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (SET startdate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (SET starttime=%%a%%b)
SET livedir="C:inetpubwwwrootMyProjectWeb"
SET stagingdir="C:inetpubwwwrootMyProjectStagingWeb"
SET livedir_revert="C:inetpubwwwrootMyProjectWebRevert"
SET backupdir="C:inetpubwwwrootMyProjectbackups%startdate%_%starttime%"
SET appcmd="C:WindowsSystem32inetsrvappcmd.exe"
SET appcmd_change=%appcmd% set vdir "MyProject/" -physicalPath:%stagingdir%
SET appcmd_revert=%appcmd% set vdir "MyProject/" -physicalPath:%livedir%
IF NOT EXIST %livedir% (
ECHO Could not find path %livedir% on %COMPUTERNAME%
GOTO End
)
IF NOT EXIST %stagingdir% (
ECHO Could not find path %stagingdir% on %COMPUTERNAME%
GOTO End
)
Choice /M "Are you ready to start?"
If Errorlevel 2 GOTO End REM Only proceed if ready, exit if user types "N"
ECHO.
ECHO Pointing website at the StagingWeb folder...
CALL %appcmd_change%
If Errorlevel 1 GOTO IISError
ECHO New site is live
ECHO.
Choice /M "Does the site still work? If NO, IIS will be reverted."
If Errorlevel 2 GOTO Revert
GOTO Backup
:Backup
ECHO Starting Web Backup to archives folder, and WebRevert in case you need to revert changes.
if exist %livedir_revert% (
rmdir %livedir_revert% /s /q
)
xcopy %livedir% %backupdir% /E /Y /q
xcopy %livedir% %livedir_revert% /E /Y /q
ECHO.
ECHO Removing old Web folder and copying StagingWeb to Web.
rmdir %livedir% /s /q
xcopy %stagingdir% %livedir% /E /Y /q
If Errorlevel 1 GOTO BackupError
ECHO.
ECHO Backup path is: %backupdir%
ECHO Backup Success! Resetting IIS to the Web/ folder...
REM Reset IIS to Web/ (which is copied from StagingWeb/)
CALL %appcmd_revert%
If Errorlevel 1 GOTO IISError
ECHO.
ECHO Great job! Now published on %COMPUTERNAME%. Don't forget to set the other live web servers!
GOTO End
:BackupError
ECHO IMPORTANT: There was an error backing up the files.
ECHO This could be caused by lack of permissions when trying to remove the old Web directory, if IIS has a lock on the folder.
ECHO Don't worry, the live site should still be ok, but you will need to manually remote into the server to sort out the backups.
GOTO End
:IISError
ECHO IMPORTANT: There was an error switching IIS over (error code = %ErrorLevel%)
ECHO Please manually remote into the server to sort things out.
GOTO End
:Revert
echo Resetting to the original Web folder...
CALL %appcmd_revert%
If Errorlevel 1 GOTO IISError
GOTO End
:End
ENDLOCAL