Thursday, April 19, 2012

Attach Debugger To All Web Processes Using Powershell Inside Visual Studio 2010

Along time ago, when the Visual Studio 2010 Powershell Console was first released I started to play around with the new capabilities and I promptly forgot about it.

I recently ran across a need to have a faster way to attach to all the web processes on my computer. I remembered that I could use Powershell to access everything in Visual Studio 2010. A little digging later I ran across the "$dte" object and remembered that this was the key to accessing everything. Using "Get-Member" several times I quickly dug into
"$dte" and got a list of local processes and found an "Attach" function on the processes object.

Here is my one-line powershell command that attaches to all IIS processes and Development Web Server processes. Doing this doesn't force you to click the warning for attaching to processes that run as different users that normally appear when using the "Attach to Process..." dialog.

$dte.Debugger.LocalProcesses | ? { ($_.Name  -like "*w3wp.exe") -or ($_.Name  -like "*WebDev.WebSe*.exe") }  | % { $_.Attach(); }

I find it as a good practice to know how to undo what I do through scripting. I normally just hit the stop debug button in Visual Studio, but below is how to detach all the debugger from the processes. Perhaps someone will find it useful.

$dte.Debugger.DetachAll();

No comments: