如何确保在Powershell中完全停止IIS网站?

问题描述 投票:5回答:3

我有一个Powershell脚本,可以停止IIS网站和相应的应用程序池,然后删除应用程序日志(log4net日志)。这是脚本片段:

stop-website -name "MyWebsite"
stop-webapppool -name "MyWebsite"
del c:\inetpub\MyWebsite\logs\*.*

问题是stop-website和stop-webapppool似乎在网站完全关闭之前返回,导致删除失败,说该文件正由另一个进程使用:

del:无法删除项目C:\ inetpub \ MyWebsite \ logs \ App.log:进程无法访问文件'C:\ inetpub \ MyWebsite \ logs \ App.log',因为它正由另一个进程使用。

如果我在stop命令和del命令之间添加10秒睡眠,则会成功删除日志。这是非常hackish但不可靠。有没有办法强制stop-website / stop-webapppool命令在网站/ apppool完全停止之前不返回?

谢谢。


从以下链接实施解决方案。我将等待~60秒然后如果它没有停止则终止IIS进程。

https://greenfinch.ie/blog/powershellscript.html

        "Stopping IIS site [$name]" >> $logFile
        stop-website -name $name

        "Stopping app pool [$name]" >> $logFile
        stop-webapppool -name $name

        $sleepTime = 5
        $processId = $TRUE
        while ($processId)
        {
            $processId = Get-WmiObject -Class win32_process -filter "name='w3wp.exe'" |
                            ?{ ($_.CommandLine).Split("`"")[1] -eq $name } |
                            %{ $_.ProcessId }

            if ($sleepTime -gt 60)
            {
                "Waited [$sleepTime] sec for process [$processId] to stop and it is still running. Killing it." >> $logFile
                Stop-Process $processId
                break
            }

            if ($processId)
            {
                "App pool [$name] is running with process ID: [$processId]. Sleeping for [$sleepTime] sec and then checking again." >> $logFile
                Start-Sleep -s $sleepTime
                $sleepTime = $sleepTime + 10
            }
        }
powershell iis
3个回答
12
投票

您可以使用这两个命令来检查网站/应用程序的状态,比如10秒后,然后使用If语句删除日志,只有当返回的状态是stopped

Get-WebsiteState -name "MyWebsite"
Get-WebAppPoolState -name "MyWebsite"

这个循环也应该对你有帮助

$currentRetry = 0;
$success = $false;
do{
    $status = Get-WebAppPoolState -name "MyWebsite"
    if ($status -eq "Stopped"){
         <....your code here....>
            $success = $true;
        }
        Start-Sleep -s 10
        $currentRetry = $currentRetry + 1;
    }
while (!$success -and $currentRetry -le 4)

更新于2019年4月24日

基于评论和当前的cmdlet document,看起来返回类型确实是一个对象。因此,大概可以作为评论或下面的行代码段来处理。作者不再能够访问Windows Server环境,因此没有直接修改原始答案也无法测试更新

if ($status.Value -eq "Stopped")

2
投票

运行“Stop-WebAppPool”后,WebAppPool的状态将为“Stopping”,并且可能需要几秒钟才能实现WebAppPool的“Stopped”状态。 这是一个帮助WebAppPoolState的小功能

function Stop-AppPool ($webAppPoolName,[int]$secs) {
$retvalue = $false
$wsec = (get-date).AddSeconds($secs)
Stop-WebAppPool -Name $webAppPoolName
Write-Output "$(Get-Date) waiting up to $secs seconds for the WebAppPool '$webAppPoolName' to stop"
$poolNotStopped = $true
while (((get-date) -lt $wsec) -and $poolNotStopped) {
    $pstate =  Get-WebAppPoolState -Name $webAppPoolName
    if ($pstate.Value -eq "Stopped") {
        Write-Output "$(Get-Date): WebAppPool '$webAppPoolName' is stopped"
        $poolNotStopped = $false
        $retvalue = $true
    }
}
return $retvalue
}

您可以使用例如

Stop-AppPool "MyWebsite" 30

并检查返回值以查看WebAppPool是否在给定的秒内停止


0
投票

以下是我使用Get-IISServerManager的方法。

$manager = Get-IISServerManager      
$site = $manager.Sites["mySiteName"]

if($site.State -ne "Stopped") {
  $site.Stop()
}

while ($site.State -ne "Stopped") {
  "waiting 1 second for site to stop..."
  Start-Sleep -s 1
}

"site stopped"
© www.soinside.com 2019 - 2024. All rights reserved.