РЕДАКТИРОВАТЬ Изменен код для отображения PID.
Редактировать 2 Изменен код для вывода уровня как %errorlevel%
См. https://docs.microsoft.com/en-us/windows/console/getconsoleprocesslist
CountConsoleProcess.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Declare Function GetConsoleProcessList Lib "Kernel32" (ByRef ProcessList as Integer, ByVal Count as Integer) As Integer
Public Sub Main ()
Dim ProcessList(0 To 9) As Integer
Dim Count As Integer
Dim Ret As Integer
Dim x as Integer
Dim ColItems as Object
Dim objWMIService As Object
objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Count = 10
'subtract one to account for this program
Ret = GetConsoleProcessList(ProcessList(0), 10) - 1
Console.Writeline("Level = " & Ret)
For x = Ret to 1 step -1
colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & ProcessList(x))
For Each objItem in colItems
Console.writeline("PID : " & objItem.ProcessID & " Command line : " & objItem.CommandLine)
Next
Next
Environment.ExitCode = Ret
End Sub
End Module
CountConsoleProcess.bat
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\CountConsoleProcess.vb" /debug:full /out:"%~dp0\CountConsoleProcess.exe" /target:exe
pause
ConsoleTest.Bat
REM Showing error level
"C:\Users\User\Desktop\ConsoleTest.bat"
Echo %errorlevel%
pause
И использовать (после выполнения cmd /k
, так что работают два CMD). Первый - это текущий PID.
Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Windows\system32>cmd /k echo hello world
hello world
C:\Windows\system32>"C:\Users\User\Desktop\ConsoleTest.bat"
C:\Windows\system32>"C:\Users\User\Desktop\Console.exe"
Level = 2
PID : 6848 Command line : "C:\Windows\System32\cmd.exe"
PID : 7824 Command line : cmd /k echo hello world
C:\Windows\system32>Echo 2
2
C:\Windows\system32>
и добавить текущую папку в путь
_AddThisFolderToPath.bat
Setx path "%path%;%~dp0"
Pause