Automatic IIS7 Deployment using MSDeploy and AppCmd

In this blogpost, I will tell you a little bit more about how to deploy your web application to IIS using MSDeploy and AppCmd. I’ll give you some information about how to create a .bat file with all the parameters you will need create a new IIS entry for your application and sync your files using MSDeploy.

Iis7

First of all, what is MSDeploy.exe aka WebDeploy? Well, MSDeploy is a server technology that enables a publishing and deployment mechanism. It enables you to not only publish files, but also create database schemas, run database change scripts, set security and much more. If you want to know more about how to install webdeploy on IIS, just take a look at the great blogpost of Scott Guthrie. 

The other tool we use is AppCmd.exe. This is the single command line tool for managing IIS 7. It exposes all key server management functionality through a set of intuitive management objects that can be manipulated from the command line or from scripts. AppCmd.exe enables you to easily control the server without using a graphical administration tool and to quickly automate server management tasks without writing code. More information can be found here.

 

Allright, What we gonna do?

  • Create new IIS site
  • Assign an application pool
  • Add HTTP binding
  • Add HTTPS binding
  • Set require SSL
  • Enable custom error mode
  • Set HTTP to HTTPS redirect
  • Sync the files from the source to the new site using msdeploy
  • Set ACL on a specifc folder

 

Step 1: Create a new batch file called f.ex. ‘NewSite.bat’. On top of it, define your application variables which will be passed trough the commandline to the batch file

::The name of the remote server
set RemoteComputer=%1

::The credentials to access the deployment services (credential manager entry) 
set RemoteCredentials=%2

::The path of where IIS sites are located like c:/Inetpub
set IISRootPath=%3

::The sources of your application to deploy
set ApplicationSourcePath=%4

::The name of your application used as IIS site name
set ApplicationName=%5

 

Step 2: Create a new batch file called f.ex. “PreSync.bat” where you define the AppCmd commands to perform the first 7 steps from the list above

::Create site directory
md %IISRootPath%%ApplicationName%

::Create site
%SYSTEMROOT%System32inetsrvappcmd add site -name:%ApplicationName% -physicalPath:%IISRootPath%%ApplicationName%

::Create application pool
%SYSTEMROOT%System32inetsrvappcmd set config -section:system.applicationHost/applicationPools /+[name='%ApplicationName%',managedRuntimeVersion='v4.0'] -commit:apphost

::Assign application pool
%SYSTEMROOT%System32inetsrvappcmd set site %ApplicationName% -[path='/'].applicationPool:%ApplicationName% -commit:apphost

::Add http binding
%SYSTEMROOT%System32inetsrvappcmd set site %ApplicationName% -+bindings.[protocol='http',bindingInformation='*:80:%ApplicationName%'] -commit:apphost

::Add https binding
%SYSTEMROOT%System32inetsrvappcmd set site %ApplicationName% -+bindings.[protocol='https',bindingInformation='*:443:%ApplicationName%'] -commit:apphost

::Set require ssl
%SYSTEMROOT%System32inetsrvappcmd set config %ApplicationName% -section:access -sslFlags:Ssl -commit:apphost

::Enable custom error mode
%SYSTEMROOT%System32inetsrvappcmd set config %ApplicationName% -section:system.webServer/httpErrors /errorMode:"Custom"

 

Step 3: Continue in the ‘NewSite.bat’ file – As we want the ‘PreSync.bat’ have the correct parameters, we generate a new .bat file called ‘PreSyncGenerated.bat’. First we remove any existing files called like that, and create a new one with the correct parameters.

::Check if GeneratedPreSync.bat and delete file
if exist PreSyncGenerated.bat (
    del PreSyncGenerated.bat
)

::Create GeneratedPreSync.bat and replace variables
for /f "tokens=*" %%a in (PreSync.bat) do call :replace "%%a"

 

Step 4: Still in the ‘NewSite.bat’, find the MSDeploy.exe location and check if MSDeploy.exe exists

::Find MsDeployPath
if "%MSDeployPath%" == "" (
    for /F "usebackq tokens=1,2,*" %%h  in (`reg query "HKLMSOFTWAREMicrosoftIIS ExtensionsMSDeploy" /s  ^| findstr -i "InstallPath"`) do (
        if /I "%%h" == "InstallPath" ( 
            if /I "%%i" == "REG_SZ" ( 
                if not "%%j" == "" ( 
                    if "%%~dpj" == "%%j" ( 
                        set MSDeployPath=%%j
                    )
                )
            )
        )
    )
)
::Check if MsDeploy.exe exists
if not exist "%MSDeployPath%msdeploy.exe" (
    echo. msdeploy.exe is not found on this machine. Please install Web Deploy before execute the script. 
    echo. Please visit http://go.microsoft.com/?linkid=9278654
    break
    pause
)

 

Step 5: Sync your application sources using MSDeply.exe. We use MSDeploy.exe to execute the AppCmd commands in the generated .bat file. Note that we skip some files and directories that we don’t want to be affected

::Sync site files
"%MSDeployPath%msdeploy.exe" -verb:sync ^
    -presync:runcommand=PreSyncGenerated.bat,waitAttempts=30 ^
    -source:contentpath=%ApplicationSourcePath% ^
    -dest:contentpath=%ApplicationName%,computerName=%RemoteComputer%,getCredentials=%RemoteCredentials% ^
    -skip:objectName=filePath,absolutePath=.*web.config$ ^
    -skip:objectName=filePath,absolutePath=.*application.config$ ^
    -skip:objectName=dirPath,absolutePath=AppData$ ^
    -skip:objectName=dirPath,absolutePath=%ApplicationName%\Css ^
    -skip:objectName=dirPath,absolutePath=%ApplicationName%\Img ^
    -skip:objectName=dirPath,absolutePath=%ApplicationName%\Configuration ^
    -skip:objectName=filePath,absolutePath=.*_App_Offline.htm$ ^
    -skip:objectName=filePath,absolutePath=.*changelog.txt$ ^

 

Step 6: Add modify rights to a folder (in our case ‘AppData’) using MSDeploy.exe. The permission will be assigned to the ‘networkservice’

::Set rights on AppData directory
"%MSDeployPath%msdeploy.exe" -verb:sync ^
                -source:setacl ^
                -dest:setacl=%IISRootPath%%ApplicationName%AppData,setAclUser=NetworkService,setAclResourceType=Directory,setaclaccess=Modify,computerName=%RemoteComputer%,getCredentials=%RemoteCredentials% ^
                -verbose

 

That’s it.. What we’ve done now is create three batch files. One called ‘NewSite.bat’ which handles the MSDeploy.exe, the second one is PreSync.bat which handles the AppCmd.exe command and a latest one which is generated from the previous file and contains the correct parameters.

Now you can just run the ‘NewSite.bat’ file using commandline with the correct parameters, and your site should be deployed on IIS. Also all files are synced from the source to the destination.

 

I hope you enjoyed this post! If you have any questions, just post a comment and I’ll be in touch.

 

Written by
Alexander Vanwynsberghe
Join the discussion

3 comments

Menu

Alexander Vanwynsberghe

Belgium-based entrepreneur. Into technology, innovation and a bit of cycling and running too. Evangelist for everything related to smart-tech.