Powershell Start-Website IIS

2.6k Views Asked by At

Problem/Question

I'm test running a script for redgate deployment. At the last step it starts a website:

import-module WebAdministration
$website = get-website | where-object { $_.name -eq $RedGateWebSiteName }

#(re)write HostName#
$binding = Get-WebBinding -Name $RedGateWebSiteName 
Set-WebBinding -Name $RedGateWebSiteName  -BindingInformation "*:80:*" -PropertyName HostHeader -Value $HostName 

if ($website) {
    Start-Website "$RedGateWebSiteName"
}

It always worked but now on one of the last days I get this error

At D:\inetpub\Timeblockr-RedGate\Api\PostDeploy.ps1:15 char:15
+     Start-Website <<<<  "$RedGateWebSiteName"
    + CategoryInfo          : InvalidOperation: (:) [Start-Website], COMException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.IIs.PowerShell.Provider.StartWebsiteCommand

IIS error

The World Wide Web Publishing Service (WWW Service) did not register the URL prefix http://*:80/ for site 3. The necessary network binding may already be in use. The site has been disabled. The data field contains the error number.

This SO question "Cannot create a file" error when running Start-Website command in Powershell gives a pretty good clue what it can be.


Edit:

After viewing my event log and checking out the answers I found out that when Redgate automaticly tries to install/start the website i'm getting this IIS error:

The World Wide Web Publishing Service (WWW Service) did not register the URL prefix http://*:80/ for site 3. The necessary network binding may already be in use. The site has been disabled. The data field contains the error number.

In this Serverfault : https://serverfault.com/questions/456217/the-world-wide-web-publishing-service-www-service-did-not-register-the-url someone its mentioning that the cause of that is that a IIS website gets created without hostheader. I'm adding the hostheader after I try to start the website. Lets find the solution

Edit 2: I was adding the hostname after I tried to start the website. This caused that the website has e empty hostname and that conflicts in IIS. (or somewhere else). I've changed my script to get the website, add the hostname and then start the website. Also added "$RedgateWebsiteName" instead of $RedGateWebSiteName. Works perfectly now!

Edit 3: After a run of tests I seem to end up the same error. The one deploy has no problems, the other deploy does has.

Edit 4: I've updated the script / Error / Post. I deleted my site from IIS after that I clicked on install. Perfectly installed - No problems, automatic start. Second run exactly the same, third run I get the error above!

3

There are 3 best solutions below

1
On

I'm guessing the $RedGateWebSiteName variable had not been assigned a value, assuming the log message replaces the variable with its' value. From googling around, it looks like the IIS "file already exists" message may be erroneous as it happens because of anything from a missing IP address binding to an invalid virtual directory folder.

1
On

Why do you need a script to start the website? If you are using Red Gate Deployment Manager, it starts the website after the deploying a web package (any package with a web.config file it).

It could be that the value substitution for $RedGateWebSiteName is not taking place and it probably needs double quotes:

Start-Website "$RedGateWebSiteName"

I say this because your error message says this:

Start-Website <<<< $RedGateWebSiteName

while the error message here displays the substituted value:

start-website <<<< "MyWebsite"

0
On

I had the exact same problem, when starting the website with:

Start-Website -Name MyWebSiteName

and it would not start. When I tried to start it manually in IIS Manager, it wouldn't start either and I got this error message:

The World Wide Web Publishing Service (W3SVC) is stopped.  Websites cannot be started unless the World Wide Web Publishing Service (W3SVC) is running.

This is pretty clear that the service is not started and it turned out to be true.