PowerShell启动IIS网站

4

问题/问题

我正在测试Redgate部署脚本。在最后一步,它会启动一个网站:

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"
}

它以前一直可以正常工作,但现在在最近的几天之一出现了这个错误。
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错误

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.

这个SO问题在PowerShell中运行Start-Website命令时出现“无法创建文件”错误提供了一个很好的线索,指明可能是什么原因。

编辑:

查看我的事件日志并检查答案后,我发现当Redgate自动尝试安装/启动网站时,我遇到了这个IIS错误:

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.

在这个Serverfault页面:https://serverfault.com/questions/456217/the-world-wide-web-publishing-service-www-service-did-not-register-the-url,有人提到这是因为创建的IIS网站没有主机头。我在尝试启动网站后添加了主机头。让我们找到解决方案。 编辑2: 我在尝试启动网站后才添加主机名。这导致网站具有空主机名,在IIS中发生冲突(或其他地方)。 我已更改我的脚本以获取网站,添加主机名,然后启动网站。 还将"$RedgateWebsiteName"添加到$RedGateWebSiteName.,现在完美运行! 编辑3: 经过一系列测试,我似乎最终遇到了同样的错误。一个部署没有问题,另一个部署有问题。 编辑4: 我已更新脚本/错误/帖子。 我从IIS中删除了我的网站,然后点击安装。完美安装-没有问题,自动启动。 第二次运行完全相同,第三次运行我收到上面的错误!

你能通过用户界面启动网站吗? - David Martin
@DavidMartin 是的,那个完美地运作了。 - JochemQuery
3个回答

1
为什么需要脚本来启动网站?如果您使用的是Red Gate部署管理器,它会在部署Web包后启动网站(任何带有web.config文件的包都可以)。可能是$RedGateWebSiteName的值替换没有发生,可能需要使用双引号:
Start-Website "$RedGateWebSiteName"
我之所以这样说,是因为您的错误消息显示如下:
Start-Website <<<< $RedGateWebSiteName 而错误消息here则显示了替换后的值:
start-website <<<< "MyWebsite"

谢谢您的回答,我尝试在它周围加上 "",然后结果是 start-website <<<< "$RedGateWebsiteName"。我也尝试让它空着,因为您说 Redgate 会自己启动它。但似乎这并没有起作用。我将查找日志文件并查看出了什么问题。 - JochemQuery

1
我猜测 $RedGateWebSiteName 变量没有被赋值,假设日志消息替换了变量的值。从谷歌搜索得知,IIS 的 "文件已存在" 消息可能是错误的,因为它可能由于缺少 IP 地址绑定或无效的虚拟目录文件夹等任何原因而发生。

你尝试过检查系统的应用程序事件日志,看看网站为什么没有启动的合理原因吗? - Wonko

-1

我曾经遇到过完全相同的问题,当我启动网站时:

Start-Website -Name MyWebSiteName

然后它就无法启动了。当我试图在IIS管理器中手动启动它时,它也无法启动,并且我收到了这个错误信息:

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

很明显,该服务没有启动,这证实了它的真实情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接