How to create Websites (and more) with PowerShell?

If you, like me, are maintaining lots of websites, you probably want to script as much as you can. It’s time consuming to change a setting for 20+ websites, and it’s also a great chance you miss something in the process.

PowerShell is really neat in these cases, and it gets better for every new release as well.

This is a script i use to do the following:

  1. Create a new application pool, with the preferred .NET version, identity and a few other settings
  2. Create a folder to hold the website
  3. Create the website
  4. Configure a scheduled task to rotate and archive log files.

This script is highly customized for my needs and my environment. As you can se there’s a lot of assumptions in the script, that you may want to change, but i may be a nice start for you!

#
# 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

#####################
# Usefull Functions #
#####################

#Get the next website-id from IIS. You should override this if you use random ids, or if your sites isn't in order!
Function Get-NextSiteId {
	$MaxId = Get-Website | Measure-Object -Property ID -Maximum
	return $id.Maximum + 1	
}

######################
# Set all parameters #
######################

#AppPool parameters

$AppPoolName 			= "Test.Domain.Com"	#Name of ApplicationPool, Should be the same as the name of the website (Eg. Soot.Msb.Se)!
$AppPoolDotNetVersion 	= "v4.0"						#.NET version for pool
$AppPoolIdentityName 	= "domain\sysAccount"			#Identity to execute pool
$AppPoolIdentityPwd 	= "P@ssw0rd"					#Identity password

#WebSite parameters
$SiteName 				= $AppPoolName					#WebSite name, by default the same as name of pool
$SiteDirectory 			= "C:\WebSite\Sites\"	#Base folder, where the site should be created. Site folder is created automatically
$SiteId					= Get-NextSiteId				#Get next free ID, change this manually if needed, eg. if you are using random id.

#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$($SiteId)"
$TaskDescription 		= "Archives old logfiles for site $($SiteName)"
$TaskActionCommand		= "C:\Program Files\LogRotate\LogRotator.App.exe"
$TaskArguments			= "C:\WebSite\Logs\W3SVC$($SiteId) C:\WebSite\Logs\Backup W3SVC$($SiteId) *.log 31 true"
$TaskPath				= "LogRotate"

###########################
# 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
#Uncomment these three lines to user AppPoolIdentity
$AppPool.processModel.identityType = 3
$AppPool.ProcessModel.Username = $AppPoolIdentityName 
$AppPool.ProcessModel.Password = $AppPoolIdentityPwd

$AppPool.ProcessModel.IdleTimeout = "0"
$AppPool.ManagedRuntimeVersion = $AppPoolDotNetVersion

#Save and start AppPool
$AppPool | Set-Item
$AppPool | Start-WebAppPool

##################
# Create website #
##################

#Create Website Directory
New-Item -ItemType directory -Path "$($SiteDirectory)$($SiteName)"

#Create Website and store it in $WebSite
New-Website -Name $SiteName -PhysicalPath "$($SiteDirectory)$($SiteName)" -ApplicationPool $AppPoolName -Port "170$($SiteId)" -HostHeader "localhost"
$WebSite = Get-Item IIS:\Sites\$($SiteName) 

#Start Website
$WebSite | Start-WebSite

######################################
# Create scheduled job for LogRotate #
# This only works on Win2012         #
######################################

$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."
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *