OdeToCode IC Logo

Powershell: Attach Debugger To ASP.NET Worker Process By Name

Thursday, August 17, 2006

For those who don't like the "Attach To Process" dialog box, just pass the application pool name to this Powershell function:

function debug-wp([string]$name)
{
  if([String]::IsNullOrEmpty($name))
  {
    throw "Usage: debug-wp -Name "<appPoolName>"
  }

  $wplist = get-wmiobject Win32_Process -f "Name='w3wp.exe'"
  foreach($wp in $wplist)
  {
    if($wp.CommandLine -match "-ap `"(.+)`"")
    {
      if($name -eq $matches[1])
      {
        & vsjitdebugger.exe -p $wp.ProcessID
        break
      }
    }
  }

  if($name -ne $matches[1])
  {
    write-host "Could not find AppPool" $name
  }
}