windows 如何使用 PowerShell 在 IIS 中停止和启动单个网站?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2710166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:17:21  来源:igfitidea点击:

How can I stop and start individual websites in IIS using PowerShell?

windowsiispowershell

提问by Joey Green

I have multiple sites configured in IIS7 on my Windows 7 development machine to run on the same port and usually only run one at a time depending on what I'm working on. I would like to be able to start and stop my development sites from PowerShell instead of having the IIS manager opened. Does anyone have a good resource to point me in the right direction or a script that already accomplishes this?

我在 Windows 7 开发机器上的 IIS7 中配置了多个站点以在同一端口上运行,并且通常一次只运行一个站点,具体取决于我正在处理的内容。我希望能够从 PowerShell 启动和停止我的开发站点,而不是打开 IIS 管理器。有没有人有很好的资源来为我指出正确的方向或已经完成此任务的脚本?

回答by Keith Hill

Just for future quick reference, the commands are:

仅供将来快速参考,命令是:

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

回答by Tim

Adding to Keith's answer, you can perform this remotely using Invoke-Command.

添加到 Keith 的答案中,您可以使用 Invoke-Command 远程执行此操作。

Import-Module WebAdministration
$siteName = "Default Web Site"
$serverName = "name"
$block = {Stop-WebSite $args[0]; Start-WebSite $args[0]};  
$session = New-PSSession -ComputerName $serverName
Invoke-Command -Session $session -ScriptBlock $block -ArgumentList $siteName 

回答by Patrick S.

To get access to system modules, Powershell needs to be run like this:

要访问系统模块,Powershell 需要像这样运行:

[path]\powershell.exe -NoExit -ImportSystemModules

I found the above on this iis forum.

我在这个iis 论坛上找到了上述内容。

回答by Russ

I found that the following to stop individual websites on a remote server to work:

我发现以下内容可以阻止远程服务器上的单个网站正常工作:

Invoke-Command -Computername $servername -Scriptblock { 
    (Import-Module WebAdministration); 
    Stop-Website -Name "WebsiteName"; 
    Stop-Website -Name "AnotherWebsiteName"
}

I had some of the errors above until Import-Modulewas put in ()

我有上面的一些错误,直到Import-Module被放入()