简体   繁体   中英

kill IIS worker process by sitename

for this question, "site names" = what IIS calls a site's name. EG, the output of:

ls IIS:\Sites\ | select Name

Given a text file with these site names, I'd like to iterate through all running w3wp.exe processes, and then kill (not recycle, kill) all w3wp.exe processes not associated with a site name in the list. (my list is NOT the output of ls IIS:\\Sites\\ | select Name btw, that's just an example to define what we are calling "site names". My list is a list of site names that should be running on the server, created manually)

I can kill all w3wp.exe's with

taskkill /IM w3wp.exe /F

but I'm struggling to make the connection between IIS site names, and their associated worker processes so I can kill specific ones based on site names. I'm guessing I need to go siteName>appPool>worker process...but I'm having a hard time.

$workerProcesses = Get-CimInstance Win32_Process -Filter "name='w3wp.exe'"
foreach ($workerProcess in $workerProcesses)
{
    $wpSiteName = $workerProcess.CommandLine.split(" ")[2].replace("`"","")
    $wpPID = $workerProcess.ProcessId

    if ($myListNotShownHere -notcontains $wpSiteName)
    {
        #taskkill /f /pid $wpPID   
    }
}

Instead of killing the worker process, you can also just stop the site. This is more performance-friendly since no process has to be killed and restarted.

As already commented by @Eris, you can do this using the WebAdministration module:

Import-Module WebAdministration
Stop-Website -Name "MySite"

You need to run PowerShell with elevated status in order to use the WebAdministration module.

If you really must kill the worker process, see stej's answer here: How to get IIS AppPool Worker Process ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM