If you, like me, create lots and lots of websites almost every day you probably noticed that a lot of the things you do are repetitive.
I have a lot of rules that almost all my websites follow.
- I name the pool, the site and the folder the same (like site.domain.com)
- I always add a binding following the pattern 17000 + site id, so a site with site id 13 has a binding for localhost:17013
- I set idle time-out to 0
- I add a scheduled recycle at 02:00 (for load balanced environments att set it at 03:00 for the other node)
- I always create a scheduled job to take care of log rotation
To avoid making misstakes, and to make sure it gets done the same way every time, i use this Powershell. It’s not perfect, but it works for me, and it does exactly what i need it to.
In the end of the script, where the scheduled job is created, i use a little application called LogRotate which i may publish on the internet. In the mean time, you can use it as a way to add a scheduled task.
Maybe it will work for you as well, or maybe you can pick the parts you need. Enjoy!
#
# PowerShell Script used to create new websites. Requires Eleveted PowerShell Console!
# This script:
# - Creates a new Applicatiopn Pool with our preferred settings
# - Creates a new website using that pool
# - Create a scheduled job to manage logfiles
#
# Make sure to edit these parameters before you run the script:
# - $AppPoolName Sets the name of AppPool and WebSite
# - $AppPoolIdentityName and $AppPoolIdentityPwd to set the identity
# - $SiteDirectory is the base path to the folder holding websites
#
#########################
# Import modules needed #
#########################
Import-Module WebAdministration
######################
# Set all parameters #
######################
#AppPool parameters
$AppPoolName = "Test.Domain.Com" #Name of ApplicationPool
$AppPoolDotNetVersion = "v4.0" #.NET version for pool
$UseAppPoolIdentity = 0 #Set to 1 to user app-pool identity instead of domain account
$AppPoolIdentityName = "domain\sysAccount" #Identity to execute pool
$AppPoolIdentityPwd = "P@ssw0rd" #Identity password
$AppPoolRecycleTime = "02:00" #Default time is 02:00, on load balanced environments, set first host to 02:00 and second to 03:00
#WebSite parameters
$SiteName = $AppPoolName #WebSite name, by default the same as name of pool
$SiteDirectory = "C:\WebSite\WebSite\Sites\" #Base folder, where the site should be created. Site folder is created automatically
###########################
# Create application pool #
###########################
#Create Pool and store it in $AppPool
New-WebAppPool ñName $AppPoolName
$AppPool = Get-Item IIS:\AppPools\$($AppPoolName)
#Stop AppPool
$AppPool | Stop-WebAppPool
#Set additional properties
#Set AppPool Identity and password
if($UseAppPoolIdentity -eq 0)
{
$AppPool.ProcessModel.identityType = 3
$AppPool.ProcessModel.Username = $AppPoolIdentityName
$AppPool.ProcessModel.Password = $AppPoolIdentityPwd
}
#Set idle time-out to zero
$AppPool.ProcessModel.IdleTimeout = "0"
#Set desired .NET version
$AppPool.ManagedRuntimeVersion = $AppPoolDotNetVersion
#Set periodic recycle schedule
Set-ItemProperty -Path IIS:\AppPools\$($AppPoolName) -Name Recycling.periodicRestart.schedule -Value @{value=$AppPoolRecycleTime}
#Save and start AppPool
$AppPool | Set-Item
$AppPool | Start-WebAppPool
#Clear periodic restart time (defaults to 1740 minutes)
Set-ItemProperty -Path IIS:\AppPools\$($AppPoolName) -Name Recycling.periodicRestart.time -Value "00:00:00"
##################
# Create website #
##################
#Create Website Directory
New-Item -ItemType directory -Path "$($SiteDirectory)$($SiteName)"
#Create Website and set default binding
New-Website -Name $SiteName -PhysicalPath "$($SiteDirectory)$($SiteName)" -ApplicationPool $AppPoolName -Port "170$($WebSite.id)" -HostHeader "" -IPAddress "*"
#Get the newly created website and store it in $WebSite
$WebSite = Get-Item IIS:\Sites\$($SiteName)
#Set additional binding based on AppPool name
New-WebBinding -Name $SiteName -IPAddress "*" -Port 80 -HostHeader $AppPoolName.ToLower()
#Start Website
$WebSite | Start-WebSite
######################################
# Create scheduled job for LogRotate #
# This only works on Win2012 #
######################################
#Scheduled task, LogRotate
#Theese values shouldn't be neccesarry to edit.
#Make sure LogRotate exists under Program Files and that you WebSite-folder point to the correct location.
$TaskName = "LogRotate W3SVC$($WebSite.id)"
$TaskDescription = "Archives old logfiles for site $($SiteName)"
$TaskActionCommand = "C:\Program Files\LogRotate\LogRotator.App.exe"
$TaskArguments = "C:\WebSite\Logs\W3SVC$($WebSite.id) C:\WebSite\Logs\Backup W3SVC$($WebSite.id) *.log 31 true"
$TaskPath = "LogRotate"
$PsHost = host
$OsVer = [environment]::OSVersion.Version
If(($OsVer.Major -ge 6) -and ($OsVer.Minor -ge 2) -and ($PsHost.Version.Major -ge 4))
{
Import-Module ScheduledTasks
$TaskAction = New-ScheduledTaskAction -Execute $TaskActionCommand -Argument $TaskArguments
$TaskTrigger = New-ScheduledTaskTrigger -Daily -AT "05:00"
$Task = New-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -Description $TaskDescription
Register-ScheduledTask $TaskName -InputObject $Task -TaskPath $TaskPath
}
Else
{
"You need a PowerShell 4 and Windows Server 2012 to create Scheduled Task!"
"You'll need to configure LogRotate manually."
}
Follow jonlin76