Justin Self
Justin Self

Reputation: 6265

Attach Debugger to multiple processes via powershell

I have several processes running that I'd like to attach to the VS debugger via powershell.

Currently, I can do this: Get-Process NServiceBus.Host | Debug-Process

If there is only one process, then I am prompted to select the correct debugger and I can continue.

However, if there are more than one processes, when I'm prompted to select a debugger for the second process, I am unable to select the currently running instance of Visual Studio.

How can I use powershell to attach multiple processes to a running instance of visual studio for debuggin?

Upvotes: 3

Views: 2407

Answers (2)

Rich Moss
Rich Moss

Reputation: 2384

Thanks, softwarebear. I was not able to get your exact command line working, but after adding curly brackets and running at an Administrator Powershell prompt, this command line successfully attached the debugger to all the running w3wp.exe processes:

$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject('VisualStudio.DTE')
$dte.Debugger.LocalProcesses | Where {$_.Name -like '*w3wp.exe'} | %{$_.Attach()}

Ideally this could be executed from within Visual Studio with a keystroke, but Visual Studio doesn't support Powershell without an add-in, and the Macro editor was removed after VS2010. Following this advice I tried adding this ScriptBlock as an 'External Tool' in a powershell.exe command line, but it didn't attach the debugger and produced no output.

Upvotes: 2

softwarebear
softwarebear

Reputation: 451

To get hold of the active visual studio instance ...

$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE")

... and to make it attach to a set of processes ...

($dte.debugger.localprocesses | Where Name -Match "proc(one|two).exe").Attach()

... it seems that a delay might be necessary between each attach if it takes too long otherwise visual studio is busy and rejects the call.

Upvotes: 6

Related Questions