Automating Website Creation with PowerShell v3

I will be the first to admit that I am not a scripting pro. Matter of fact, I am pretty much a novice at it. Recently, I stumbled across a PowerShell script that creates a website, populates it with default text page, and launches IE to test the page. This script was written for PowerShell v2. I have thought about the script a few times and decided to sit down and port it over to PowerShell v3. All I can say is I am glad I did. Using the ISE, Integrated Scripting Environment, for PowerShell v3 made me feel like a real scripter.

This was the original script that I put together following the guide above.

Set-ExecutionPolicy RemoteSigned
Import-Module WebAdministration
$webroot=New-Item c:\PSCreatedSite -type Directory
New-Item c:\PSCreatedSite\PSCreatedApp -type Directory
Set-Content c:\PSCreatedSite\default.htm “PSCreated Default Page”
Set-Content c:\PSCreatedSite\PSCreatedApp\default.htm “PSCreated\PSCreatedApp Default Page”
New-Item iis:\apppools\PSCreatedSite
New-Item iis:\Sites\PSCreatedSite -physicalpath $webroot -bindings @{protocol=”http”;bindingInformation=”:81:”}
Set-ItemProperty iis:\Sites\PSCreatedSite -Name applicationpool -Value PSCreatedSite
New-Item iis:\sites\pscreatedsite\pscreatedapp -physicalpath c:\pscreatedsite\pscreatedapp -type Application
Set-ItemProperty iis:\sites\pscreatedsite\pscreatedapp -Name applicationpool -Value PSCreatedSite
#
#now for the automated test as well
$ie=New-Object -com internetexplorer.application
$ie.visible = $true
$ie.Navigate(“http://localhost:81/”)

I updated all of the New-Item commands that referenced the IIS: drive and and changed them to use the New-WebAppPool and New-Website cmdlets. Using the new Commands Add-On, I was able to select the New-WebSite command, for instance, and see the parameters that were required. As you can see, the only required parameter to create a new website is the Name. Since I am going to be creating multiple sites on a test server that only has 1 assigned IP, I want to include HostHeader, and PhysicalPath as well. It is also a best practice to create application pools for each website, so I specified that as well in my command. I then used the Insert capability to insert the complete command into the console where I could copy and paste it into my script. This GUI ensures that the syntax is correct when you insert it into your script.

new-website_command

I decided I also wanted to update the script to include the ability to parse a csv file to provide the input for the Website creation commands. I went with a very generic list. I created the csv file with 2 variables: Website and Path. Here is a sample of the contents of the websites.csv file that I created. By using the localtest.me domain, there is no need to create host file entries to test your local websites. This domain resolves back to 127.0.0.1. If you would like additional information related to localtest.me, check out Scott Forsyth’s blog post.

Website,path
“test1.localtest.me”,”c:\inetpub\test1.localtest.me”
“test2.localtest.me”,”c:\inetpub\test2.localtest.me”
“test3.localtest.me”,”c:\inetpub\test3.localtest.me”

And finally, the thing you have read this whole document for, the script itself. Smile

<#
This can be used to script the creation of Websites. It was written using PowerShell v3 on Windows Server 2012 for IIS8. It uses the WebAdministration module that is one of many modules included in this new release. Use at your own risk. Once you copy it, you own it.

by Terri Donahue
#>

Import-Module WebAdministration
Import-Csv “C:\Scripts\websites.csv” | ForEach-Object {
$WebSiteName = $_.Website
$PathInfo = $_.path
New-Item $PathInfo -ItemType Directory
New-WebAppPool -Name $WebSiteName
New-Website -Name $WebSiteName -ApplicationPool $WebSiteName -HostHeader $WebSiteName -PhysicalPath $PathInfo
Set-Content $PathInfo\default.htm “PSCreated Default Page”
}

This is just a sample script, but as you see, it is quite powerful. Since I didn’t specify the WebSite ID, it was automatically generated starting with the next available number. The application pool was created using the Application Pool Defaults. All of these things could have been included in the script and set according to required specifications. I used the $WebSiteName to populate the name of the site, the host header and also the application pool name. This just provides consistency if you are managing a large number of websites on a server. If your directory structure already exists, you could populate the existing path in your websites.csv file and remove the New-Item $PathInfo -ItemType Directory line of the script. You would also want to remove the Set-Content line to ensure that you do not overwrite any instance of default.htm if your code is already deployed.

Here is a screenshot of IIS Manager after running the script that shows the application pools as well as the websites that were created.

image

I hope you find this information helpful and look forward to my next trip into the scripting wilds.

Terri is a Support Specialist at OrcsWeb, a hosted server company providing managed hosting solutions.

Be Sociable, Share!

    7 Comments.

    1. Automating Website Creation with PowerShell v3 - Web and Cloud - pingback on October 5, 2012 at 8:47 pm
    2. Automating website creation with PowerShell | Terri Donahue - pingback on October 6, 2012 at 11:20 am
    3. Thanks for sharing..
      Terri,
      do you have any automated script for building IIS 7.5

      Thanks :smile:

    4. Terri,

      can we use the same script for IIS 7.5

      • Yes, this script can be used with IIS 7.5 as well. It is also backwards compatible with PowerShell v2 in case you have not upgraded to Powershell v3 yet.

    5. Terri It’s really awesome post thanks for sharing :grin: