3

I'm trying to use the following PowerShell script but get Length cannot be zero error:

Import-Module WebAdministration

$xml = [xml](Get-Content .\settings.xml)

$websites = $xml.Websites

foreach ($website in $websites.Website){    
   $dnsName = $website.DnsName

....

   #check if the site exists
   if (!(Test-Path "IIS:\Sites\$iisAppName" -pathType container))
   {
    $website = "IIS:\Sites\$($iisAppName)"

    $httpsBindingInformation = "*:443:"+$dnsName
    $bindings = @(@{protocol="net.tcp";bindingInformation="808:*"},@{protocol="https";bindingInformation=$httpsBindingInformation;sslFlags=1})

    $physicalPath = "$($directoryPathPrefix)$($iisAppName)"


    Write-Host "Creating website $($iisAppName)"
    #create the site
    $iisApp = New-Item -path $website -bindings $bindings -physicalPath $physicalPath
   }
}

Full error message is following:

New-Item : Length cannot be less than zero.
Parameter name: length
At D:\CreateWebSites.ps1:51 char:13
+         $iisApp = New-Item -path $website -bindings $bindings -physicalPath $physicalP ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Item], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : Length cannot be less than zero.
Parameter name: length,Microsoft.PowerShell.Commands.NewItemCommand

I cannot find out which parameter it is complaining about. New-Item has no parameter Length... Any hints?

Azimuth
  • 2,599
  • 4
  • 26
  • 33
  • You need to supply a `Name` for the new item, using the `-Name` parameter – Mathias R. Jessen Dec 03 '15 at 12:59
  • @MathiasR.Jessen in every example I find on Internet, there is no `Name` parameter. I tried to add `-Name` but then I get `Invalid site name` – Azimuth Dec 03 '15 at 13:15
  • Have you tried `New-Item -Path "IIS:\Sites\" -Name $iisAppName -physicalPath $physicalpath`? – Mathias R. Jessen Dec 03 '15 at 13:17
  • @MathiasR.Jessen I get `Item type cannot be resolved from parent path \\Servername` if I try that – Azimuth Dec 03 '15 at 13:20
  • Hopefully you have already figured this out, but if not, have you tried stepping through your code? After assigning the $xml variable, do you have data there? Does $websites have data? Without your XML I'd guess the issue is here "$website in $websites.Website", I suspect it should be "$website in $websites". – DBADon Apr 26 '19 at 16:35

0 Answers0