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
{
 param([string]$name)
 if(-not(Get-Module -name $name))
 {
 if(Get-Module -ListAvailable | Where-Object { $_.name -eq $name })
 {
 Import-Module -Name $name
 }
 }
}

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

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

 $existingWebSite = Get-Website | Where-object { $_.name -eq $name }
 if($existingWebSite)
 {
 $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,
 [string]$propertyName,
 [string]$propertyValue)

 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."
if(!$basePath)
{
 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"
 exit
}

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"

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 WordPress.com.

%d bloggers like this: