Martin M
Martin M

Reputation: 465

Stop IIS express process based on site name

The site im working on got 3 individual sites running on the IIS. When I make changes to on particular library I need to restart the site using it. The way I do that now is by manually rightclicking the IIS Express icon in the system tray, and then clicking 'Stop site', and after that I execute the debugging..

I would like to make that part automatic, so when ever i start debugging it will stop that particular site. If I don't stop it, then it will just reuse the current running site, but if I stop it, then it will restart it..

Is it event posible? I know how to find the PID, but I don't get the name of the site behind the PID..

Upvotes: 1

Views: 1637

Answers (1)

Rodney Viana
Rodney Viana

Reputation: 544

I put together this script in PowerShell:

$site = 'Webapplication' # replace this by your site name (not case sensitive)
$process = Get-CimInstance Win32_Process -Filter "Name = 'iisexpress.exe'" | ? {$_.CommandLine -like "*/site:`"$site`"*" }
if($process -ne $null)
{
    Write-Host "Trying to stop $($process.CommandLine)"
    Stop-Process -Id $process.ProcessId
    Start-Sleep -Seconds 1 # Wait 1 second for good measure
    Write-Host "Process was stopped"
} else
{
   Write-Host "Website was not running"
}
  • Modify the first line to replace the site name with yours. Save this file as stopiis.ps1 on your project folder (not the solution folder).
  • Now, on Solution Explorer, right-click and choose properties
  • On the left side, choose Build Events
  • Put this on 'Pre-Build event command line' so it will run before compiling:

    echo powershell -File "$(ProjectDir)stopiis.ps1"

    powershell -File "$(ProjectDir)stopiis.ps1"

Build Action

  • Note: you do not need to run Visual Studio in Administrative mode because IISExpress.exe run under your account

Upvotes: 3

Related Questions