3

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?

Justin Self
  • 6,137
  • 3
  • 33
  • 48
  • I have never known or attempted to attach more than one process to VS debugger. I simply don't think it can be done. – JWP Feb 23 '15 at 21:09
  • @JohnPeters I do several times a day through the VIsual Studio Debug > Attach to Process. – Justin Self Feb 23 '15 at 21:13
  • Wow! VS allows multiple process debugging at one time? – JWP Feb 23 '15 at 21:15
  • @JohnPeters Yup. One way is if you use 'Multiple Startup Projects' at the solution level and start debugging (F5), then you are debugging multiple processes. – Justin Self Feb 23 '15 at 21:31

2 Answers2

6

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.

softwarebear
  • 451
  • 4
  • 5
  • Thanks for this hint, but somehow it is not working properly. I get this error: + ($dte.debugger.localprocesses | Where <<<< Name -Match "proc(one|two).exe").Attach() + CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand – etalon11 Dec 23 '15 at 08:35
  • That second line needs to be something like this ... I imagine I only had one process found at the time I ran it ... suspect not zero or many processes might cause the error you saw etalon11. $dte.debugger.localprocesses | Where Name -Match "proc(one|two).exe" | % { $_.Attach() } – softwarebear Sep 20 '17 at 08:57
2

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.

Rich Moss
  • 2,195
  • 1
  • 13
  • 18