Automating Website Creation with PowerShell v3

by Terri

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值