I created a function to create a site in IIS, but I"m running into a bizzare error.
Here's the url I've been using as a reference:
http://learn.iis.net/page.aspx/433/powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools/
New-Item : Index was outside the bounds of the array.
At line:1 char:9 + New-Item <<<< 'IIS:\Sites\SiteName' -physicalPath "$sitePath" -bindings @{protocol="$protocol";bindingInformation="$fullBindings"} + CategoryInfo : NotSpecified: (:) [New-Item], IndexOutOfRangeException + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Commands.NewItemCommand
Here's the code block that calls the function:
    function Create-FullSite($site, $framework, $userName, $password, $protocol, $port, $enabledProtocols)
            {
                #Write-Host "Prompting for path to "
                $sitePath = Select-Folder
                #Write-Host $sitePath
                #Write-Host "Setting up app pool for "
                $csServicePool = New-Item -Path iis:\AppPools\$site
                #Write-Host "Configuring app pool"
                Set-ItemProperty -Path IIS:\AppPools\$site -name managedRuntimeVersion -value $framework
                $csServicePool.processModel.username = $userName
                $csServicePool.processModel.password = $password
                $csServicePool.processModel.identityType = 3
                $csServicePool  | set-item 
                #Write-Host "Creating IIS Site "
                $fullBindings = ':'+$port.ToString()+':'
                Write-Host $fullBindings
                Write-Host $site    
                Write-Host $sitePath
                Write-Host $protocol
                New-Item IIS:\Sites\$site -physicalPath "$sitePath" -bindings @{protocol="$protocol";bindingInformation="$fullBindings"}
                #Write-Host "Assigning App pool to "
                Set-ItemProperty -Path IIS:\Sites\$site -name ApplicationPool -value $site
                #Write-Host "setting applicationDefaults.enabledProtocols: "
                Set-ItemProperty -Path IIS:\Sites\$site -name applicationDefaults.enabledProtocols -value "$enabledProtocols"
                return $sitePath
            }   
    $ServicesSiteName = 'MyNewSite'
            $ServicesPort = '80'
            $ServiceBindings = 'http'
            $csWebServiceUserName = 'domain\someUser'
            $csWebServicePassword = 'AReallyComplexPassword'
            $v2Framework = 'v2.0'
            $v4Framework = 'v4.0'
           Create-FullSite $ServicesSiteName $v2Framework $csWebServiceUserName $csWebServicePassword $ServiceBindings $ServicesPort $ServiceBindings
It turns out if you delete all the websites in IIS, the "Index Out of Range" exception is always thrown. I have a feeling it's trying to generate a site id and cannot find the next one in the list. This article helped me solve the issue. http://forums.iis.net/t/1159761.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With