ILearnable .Net

February 20, 2014

set up a new dev machine IIS sites through script

Filed under: Uncategorized — andreakn @ 14:37

here’s a powershellscript you can use to script IIS web sites (we update it whenever we add new web sites to the project so everyone is running their sites on the same ports and visual studio doesn’t complain)

param( [string]$basePath)

#Imports a module if it isn't already imported
function Using-Module
 if(-not(Get-Module -name $name))
 if(Get-Module -ListAvailable | Where-Object { $ -eq $name })
 Import-Module -Name $name

#Creates a website. Overwrites if website exists.
function Create-WebSite
 param( [string]$name,

 Write-Host "Creating web site $name on $port with physical path $physicalPath"

 $existingWebSite = Get-Website | Where-object { $ -eq $name }
 $existingAppPool = $existingWebSite.applicationPool
 Remove-Website $name
 Remove-WebAppPool $existingAppPool

 $appPoolName = $name + "_AppPool"

 New-WebAppPool $appPoolName
 #New-WebSite -Name $name -Port $port -HostHeader $name -PhysicalPath $physicalPath -ApplicationPool $appPoolName
 New-WebSite -Name $name -Port $port -PhysicalPath $physicalPath -ApplicationPool $appPoolName

#Modify app pool property
function Modify-AppPoolProperty
 param( [string]$name,

 Set-ItemProperty "IIS:\AppPools\$name" -name $propertyName -value $propertyValue

Write-Host "Creating IIS web sites and application pools for alle web and service projects. Existing web sites and application pools will be replaced"
Write-Host "Specify base path needs to be specified as file argument."
 Write-Host "Base path not specified. Syntax: CreateWebSites -basePath c:\root\of\sourcecode"
 Write-Host "where c:\root\of\sourcecode is where you checkout the code to"

Using-Module -name WebAdministration
Create-WebSite -name myWebSite -port 8000 -physicalPath "$basePath\src\SomeAspect\SomeSolution\SomeIisWebProject\"
Modify-AppPoolProperty -name myWebSite_AppPool -propertyName "managedRuntimeVersion" -propertyValue "v4.0"

....lather, rinse, repeat

Write-Host "Creating web sites completed"


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: