search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Creating IIS7 Web Site with Powershell
Posted in Hosting & IIS7, Windows Powershell | 4 Comments | 11,530 views | 11/12/2010 01:25

Detailed script about how to create website in IIS7 with Powershell.

1
2
3
4
5
6
7
Function Create-WebSite
{
Param ($LogDir, $Description, $Domain)
 
     $Domain = ($Domain.TrimStart("www")).TrimStart(".")
     New-Item IIS:\Sites\$Description -Bindings (@{Protocol="http";BindingInformation="*:80:$Domain"},@{Protocol="http";BindingInformation="*:80:www.$Domain"}) -PhysicalPath "$LogDir\http"
}

$LogDir is the path of FTP directory and $Description is the name of the website.


Comments (4)

David Penn

February 16th, 2011
19:51:14

This is exactly what I’m looking for but it doesn’t work on IIS7 64-bit (maybe on no other OS either). It complains that it is missing parameter Bindings. It is as if Powershell thinks the -Bindings is a parameter that someone should have input like it was a $. Please help.


admin

February 16th, 2011
20:41:21

Hello David,

I wrote all scripts on Windows Server 2008 R2 which is 64bit only. Do you execute it like this?

Create-Website -LogDir "C:inetpubyusufozturk.info" -Description "www.yusufozturk.info" -Domain "yusufozturk.info"

If you do same thing, could you write output of Powershell console. So we can debug it.


David Penn

February 17th, 2011
00:38:11

I’m new to PowerShell so I may be doing this wrong. I saved your script to a .ps1 file. I then created another to test it in which I put:

. F:\scripts\createiis7website.ps1
Create-Website -LogDir “C:\inetpub\yusufozturk.info” -Description “www.yusufozturk.info” -Domain “yusufozturk.info”

Running this with .\cws produces:

New-Item : A parameter cannot be found that matches parameter name ‘Bindings’.
At F:\scripts\createiis7website.ps1:5 char:45
+ New-Item IIS:\Sites\$Description -Bindings <<<< (@{Protocol="http";BindingInformation="*:80:$Domain"},@{Protocol="http";BindingInformation="*:80
:www.$Domain"}) -PhysicalPath "$LogDir\http"
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

We have a number of websites that we have to register and in IIS6 you could just run a VBS file which would load the XML of the website in and it would reset the path for them when we changed working directories.

Now that we are on IIS7, that easy functionality seems very difficult. This script made me think we could right a powershell script which we'd pass the path of the root in and it would use your script to register all the websites.

Thanks so much for your help.


admin

February 20th, 2011
12:06:22

Oh I see.

You can use 2 different ways with Powershell.

1) You can use function in a .ps1 file.
2) You can use .ps1 file as a function.

For example, if you want to go with first option.

1) You have a .ps1 file called “create.ps1”. The codes of it:

1
2
3
4
5
6
Function Create-WebSite
{
Param ($LogDir, $Description, $Domain)
$Domain = ($Domain.TrimStart("www")).TrimStart(".")
New-Item IIS:Sites$Description -Bindings (@{Protocol="http";BindingInformation="*:80:$Domain"},@{Protocol="http";BindingInformation="*:80:www.$Domain"}) -PhysicalPath "$LogDirhttp"
}

So, If you want to use Create-Website function, you should do:

1
2
. .create.ps1 (to import function into environment)
Create-Website -LogDir "C:inetpubyusufozturk.info" -Description "www.yusufozturk.info" -Domain "yusufozturk.info"

You see? If you want to use Function, first you should import .ps1 file.

2) But if you want to go with second way, create a .ps1 file again, called create.ps1 and use this codes:

1
2
3
4
$Logdir = $args[0]
$Description = $args[1]
$Domain = $args[2]
New-Item IIS:Sites$Description -Bindings (@{Protocol="http";BindingInformation="*:80:$Domain"},@{Protocol="http";BindingInformation="*:80:www.$Domain"}) -PhysicalPath "$LogDirhttp"

To execute new .ps1 file, just use it like this:

.create.ps1 "C:inetpubyusufozturk.info" "www.yusufozturk.info" "yusufozturk.info”

You understand differences? You can search in this website about $args and functions to understand usage.



Leave a Reply