Configuring IIS 7 script with APPCMD – Altiris Script

Recently I was required to create a re-runable script which would configure an IIS web site and Application Pool. This script required to be re-runable to correct any inconsistencies from web server to web server.

The delivery method was to use Altiris to  deploy to numerous servers at once.

This script uses the Altitis WlogEvents.exe to pass back error and informational status to Altiris for collection and evaluation.

Although this configures HttpS and binds the site to an https IP address I am yet to configure the script to assign the correct Certificate to the site

Code Below…


REM Configure IIS Settings

Set AppPool=AppPoolName
Set WebSite=WebSiteName
Set IPAddress=IPAddressOfSite
Set managedPipelineMode=Integrated
Set enable32BitAppOnWin64=false
Set physicalPath=c:\InetPub\%WebSite%

Set APPCMD="%systemroot%\system32\inetsrv\APPCMD"
Set WlogEvents=C:\Software\Installers\Altiris\WLogevent.exe

Call :Log %ERRORLEVEL% "AppPool: %AppPool%"
Call :Log %ERRORLEVEL% "WebSite: %WebSite%"
Call :Log %ERRORLEVEL% "IPAddress: %IPAddress%"

If Not Exist "%physicalPath%" mkdir "%physicalPath%"
Call :Log %ERRORLEVEL% "Creating Directory if not exist: %physicalPath%"

REM Create App Pool if not exists
%APPCMD% list AppPool "%AppPool%"
If %ERRORLEVEL% neq 0 ( Call :CreateAppPool )

REM enable32BitAppOnWin64
%APPCMD% Set APPPOOL /Apppool.name:%AppPool% /enable32BitAppOnWin64:%enable32BitAppOnWin64%
Call :Log %ERRORLEVEL% "Setting enable32BitAppOnWin64 to %enable32BitAppOnWin64%"

REM managedPipelineMode
%APPCMD% Set APPPOOL /Apppool.name:%AppPool% /managedPipelineMode:%managedPipelineMode%
Call :Log %ERRORLEVEL% "Setting managedPipelineMode to %managedPipelineMode%"

REM Configure Recycle pool time
%APPCMD% list apppool "%AppPool%" /config | findstr "04:"
If %ERRORLEVEL% neq 0 ( Call :ConfigRecyclePool )

REM Create WebSite if not exist
%APPCMD% list site "%WebSite%"
If %ERRORLEVEL% neq 0 ( Call :CreateWebSite )

REM Configure Bindings for Http
%APPCMD% list site  "%WebSite%" /text:*|findstr "bindingInformation:""%IPAddress%:80
If %ERRORLEVEL% NEQ 0 ( Call :ConfBindingsHttp ) else (
Call :Log %ERRORLEVEL% "Skipping Existing protocol='http',bindingInformation='%IPAddress%:80:"
)

REM Configure Bindings for Https
%APPCMD% list site  "%WebSite%" /text:*|findstr "bindingInformation:""%IPAddress%:443
If %ERRORLEVEL% NEQ 0 ( Call :ConfBindingsHttps ) else (
Call :Log %ERRORLEVEL% "Skipping Existing protocol='https',bindingInformation='%IPAddress%:443:"
)

REM Change the physical Path location of files
%APPCMD% set site /site.name:"%WebSite%" /[path='/'].[path='/'].physicalPath:"%physicalPath%"  /commit:apphost
Call :Log %ERRORLEVEL% "physicalPath: %physicalPath%"

REM Assign site to app pool %AppPool%
%APPCMD% set site /site.name:"%WebSite%" /[path='/'].applicationPool:%AppPool% /commit:apphost
Call :Log %ERRORLEVEL% "Assign %WebSite% to applicationPool: %AppPool%"

Call :Log %ERRORLEVEL% "End of Script"
Exit

:CreateAppPool
%APPCMD% set config  -section:system.applicationHost/applicationPools /+"[name='%AppPool%']" /commit:apphost
Call :Log %ERRORLEVEL% "Creating App Pool %AppPool%"
GOTO:EOF

:CreateWebSite
%APPCMD% add site /name:"%WebSite%" /physicalPath:"%physicalPath%" /bindings:"http/%IPAddress%:80:"
Call :Log %ERRORLEVEL% "Creating Web Site %WebSite% to %physicalPath% with bindings http/%IPAddress%:80"
GOTO:EOF

:ConfBindingsHttp
%APPCMD% set site /site.name:"%WebSite%" /+bindings.[protocol='http',bindingInformation='%IPAddress%:80:'] /commit:apphost
Call :Log %ERRORLEVEL% "protocol='http',bindingInformation='%IPAddress%:80:"
GOTO:EOF

:ConfBindingsHttps
%APPCMD% set site /site.name:"%WebSite%" /+bindings.[protocol='https',bindingInformation='%IPAddress%:443:'] /commit:apphost
Call :Log %ERRORLEVEL% "protocol='https',bindingInformation='%IPAddress%:443:"
GOTO:EOF

:ConfigRecyclePool
Call :GenerateRecycleTime
%APPCMD% Set APPPOOL /Apppool.name:%AppPool% /+recycling.periodicRestart.schedule.[value='%RANDTIME%']
Call :Log %ERRORLEVEL% "Apppool.name:%AppPool% /+recycling.periodicRestart.schedule.[value='%RANDTIME%']"
%APPCMD% set config  -section:system.applicationHost/applicationPools /[name='%AppPool%'].recycling.periodicRestart.time:"00:00:00"  /commit:apphost
Call :Log %ERRORLEVEL% "applicationPools /[name='%AppPool%'].recycling.periodicRestart.time:00:00:00"
GOTO:EOF

:GenerateRecycleTime
REM Random Time - 4am
SET RANDTIME=%time:~7,1%
SET RANDTIME=%RANDTIME%%time:~10%
IF %RANDTIME% GTR 59 GOTO :GenerateRecycleTime
SET RANDTIME=04:%RANDTIME%:00
GOTO:EOF

:Log
If %1 neq 0 (Set Level=3) else (Set Level=1)
%WlogEvents% -id:%ID% -n:"ConfigIIS" -c:"%1" -l:%Level% -ss:%2
GOTO:EOF

Tags: , , ,

Leave a Reply