DEV Community

Cover image for Create IIS application pool and application via command line
Miro Grenda
Miro Grenda

Posted on

Create IIS application pool and application via command line

Let's say you have an existing IIS site and want to add an application to it. Usually you open up the Internet Information Services (IIS) Manager and do it there using the context menu.

But now assume - and that was in my case - you have multiple servers hosting the same site (behind a load balancer). Depending of the number of servers this is going to take a while, is not really exciting and maybe error prone.

I thought there should be a better solution - and yes, there is one, the command line tool for managing IIS - see https://docs.microsoft.com/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe

So I've created a batch file (.bat) which creates an application pool, sets the recycling option, creates an app inside of an existing site and then sets the application pool on the new app:

@ECHO OFF

ECHO # CREATE APP POOL
%systemroot%\system32\inetsrv\appcmd add apppool /name:app1 /managedRuntimeVersion:"v4.0" /managedPipelineMode:"Integrated"

ECHO # SET APP POOL RECYCLING
%systemroot%\system32\inetsrv\appcmd clear config -section:system.applicationHost/applicationPools /[name='app1'].recycling.periodicRestart.schedule 
%systemroot%\system32\inetsrv\appcmd set config -section:system.applicationHost/applicationPools /+[name='app1'].recycling.periodicRestart.schedule.[@0,value='03:00:00'] /commit:apphost
%systemroot%\system32\inetsrv\appcmd set config -section:system.applicationHost/applicationPools /[name='app1'].recycling.periodicRestart.time:00:00:00 /commit:apphost

ECHO # CREATE APP
MD D:\@Work\DEV\IIS\example-site\app1
%systemroot%\system32\inetsrv\appcmd add app /site.name:example-site /path:/app1 /physicalPath:D:\@Work\DEV\IIS\example-site\app1

ECHO # SET APP POOL FOR APP
%systemroot%\system32\inetsrv\appcmd set app "example-site/app1" /applicationPool:app1

I run it - and checked the result via IIS manager - and as expected everything was setup correctly. Here are the screenshots:

Alt Text
Alt Text
Alt Text
Alt Text

Top comments (0)