dcandygmailcom

Like Unix's $ShLvl List processes in Windows' command prompt

Feb 1st, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.06 KB | None | 0 0
  1. REM Three files follow
  2. REM ListConsole.bat
  3. REM This file compiles ListConsole.vb to ListConsole.exe using the system VB.NET compiler.
  4. REM ListConsole.exe list the processes in the console and returns an errorlevel saying how many.
  5. C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\ListConsole.vb" /out:"%~dp0\ListConsole.exe" /target:exe
  6.  
  7. --------------------------------------------------
  8.  
  9. REM ListConsoleTest.bat
  10. REM Shows ListConsole.exe in action.
  11. "%~dp0\ListConsole.exe"
  12. Echo Number of Processes is %errorlevel%
  13. pause
  14.  
  15. --------------------------------------------------
  16. Sample Output
  17.  
  18. Level = 2
  19. PID : 6380  Command line : C:\Windows\system32\cmd.exe /c ""C:\Users\User\Desktop\Bat+Vbs\ListConsole\ListConsole
  20. Test.bat" "
  21. PID : 4320  Command line : cmd  /c "C:\Users\User\Desktop\Bat+Vbs\ListConsole\ListConsole.exe"
  22.  
  23. --------------------------------------------------
  24.  
  25. 'ListConsole.vb
  26. imports System.Runtime.InteropServices
  27. Public Module MyApplication  
  28.  
  29. Public Declare Function GetConsoleProcessList Lib "Kernel32" (ByRef ProcessList as Integer, ByVal Count as Integer) As Integer
  30.        
  31. Public Sub Main ()
  32.                 Dim ProcessList(0 To 9) As Integer
  33.                 Dim Count As Integer
  34.                 Dim Ret As Integer
  35.                 Dim x as Integer
  36.         Dim ColItems as Object
  37.                 Dim objWMIService As Object
  38.                 objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  39.                 Count = 10
  40.                 'subtract one to account for this program
  41.                 Ret = GetConsoleProcessList(ProcessList(0), 10) - 1
  42.                 Console.Writeline("Level = " & Ret)
  43.  
  44.                 For x = Ret  to 1 step -1
  45.                     colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & ProcessList(x))
  46.                     For Each objItem in colItems
  47.                         Console.writeline("PID : " & objItem.ProcessID & "  Command line : " &  objItem.CommandLine)
  48.                     Next
  49.                 Next
  50.                 Environment.ExitCode = Ret  
  51. End Sub
  52. End Module
Advertisement
Add Comment
Please, Sign In to add comment