5

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?

0

3 Answers 3

10

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

4

This can also be circumvented by using:

New-Website -Name Blah -Id [xxx]

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

3

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
1
  • Thanks for posting your solution. Worked like a champ. Well done. Nov 4, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.