Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"PS c:\> New-WebSite -Blah:" throws Index was outside the bounds of the array

On one of my servers the command New-WebSite stopped working today (it was working fine yesterday), throwing the exception "Index was outside the bounds of the array".

PS C:\Windows\system32> Import-Module WebAdministration
PS C:\Windows\system32> New-WebSite -Blah
New-Item : Index was outside the bounds of the array.
    + CategoryInfo          : NotSpecified: (:) [New-Item], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Commands.NewItemCommand

Does anyone know what might have caused this?

like image 872
Peter Moberg Avatar asked Aug 26 '10 09:08

Peter Moberg


3 Answers

This is a bug in the New-WebSite commandlet. Apparently there must be at least one site configured in IIS otherwise New-WebSite crashes.

like image 130
Peter Moberg Avatar answered Sep 29 '22 18:09

Peter Moberg


This can also be circumvented by using:

New-Website -Name Blah -Id [xxx]

See: http://forums.iis.net/t/1159761.aspx

like image 31
Richard Lennox Avatar answered Sep 29 '22 16:09

Richard Lennox


As mentioned and accepted this is a bug that comes up when creating a new site when IIS has no existing sites. Since this isn't very helpful in solving the problem here's the solution I found while digging through the forum listed in another answer:

#This line will get the highest id of any existing sites and add one to it (or start you off at 1)
$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
$webSite = New-Website -Name "$name" -PhysicalPath "$physicalPath" -ApplicationPool "$applicationPool" -Port "$port" -IPAddress "$IPAddress" -HostHeader "$hostName" -id $id
like image 40
Kevin T Avatar answered Sep 29 '22 17:09

Kevin T