Advertisement
AtZako

Visual Studio - Attach to IIS worker process macro

Feb 17th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.95 KB | None | 0 0
  1. 'Run this to get all IIS worker processes: %windir%\system32\inetsrv\appcmd.exe list wp
  2. 'How to attach hotkey for macros: http://blog.lavablast.com/post/2008/01/11/Attach-to-Process-with-one-shortcut.aspx
  3. Imports System
  4. Imports EnvDTE
  5. Imports EnvDTE80
  6. Imports EnvDTE90
  7. Imports EnvDTE90a
  8. Imports EnvDTE100
  9. Imports System.Diagnostics
  10. Imports System.IO
  11.  
  12. Public Module HotKeys
  13.     Sub AttachToIIS_WP()
  14.         Const IISPoolName = "Your IIS app pool name"
  15.         Dim pathToExe As String = Path.Combine(Environment.GetEnvironmentVariable("windir"), "system32\inetsrv\appcmd.exe")
  16.         Dim arguments As String = "list wp"
  17.         Dim workerProcesses As String = RunConsoleApp(pathToExe, arguments)
  18.         Dim procId As Integer = 0
  19.         For Each wp As String In workerProcesses.Split(Environment.NewLine)
  20.             If (wp.Contains(IISPoolName)) Then
  21.                 Dim procIdString As String = wp.Split(" ")(1).Replace("""", "")
  22.                 procId = Integer.Parse(procIdString)
  23.                 Exit For
  24.             End If
  25.         Next
  26.  
  27.         For Each proc In DTE.Debugger.LocalProcesses
  28.             If (proc.ProcessID = procId And proc.Name.Contains("w3wp.exe")) Then
  29.                 proc.Attach()
  30.                 Exit Sub
  31.             End If
  32.         Next
  33.     End Sub
  34.  
  35.     Function RunConsoleApp(ByVal pathToExe As String, ByVal arguments As String)
  36.         Dim returnvalue As String = ""
  37.         Dim info As ProcessStartInfo = New ProcessStartInfo(pathToExe, arguments)
  38.         info.UseShellExecute = False
  39.         'info.RedirectStandardInput = True
  40.         info.RedirectStandardOutput = True
  41.         info.CreateNoWindow = True
  42.  
  43.         Using process As System.Diagnostics.Process = process.Start(info)
  44.             'Dim sw As StreamWriter = process.StandardInput
  45.             Dim sr As StreamReader = process.StandardOutput
  46.             returnvalue = sr.ReadToEnd()
  47.         End Using
  48.  
  49.         Return returnvalue
  50.     End Function
  51. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement