我最近运行了一个在其中运行“ cmd”的第3方脚本,从而增加了cmd窗口的嵌套深度(这使我的历史记录和DOSKey宏在该过程中不可用)。所以我想知道是否有等效的$ SHLVL或其他方法来确定这种情况?我想我可以向上箭头查看我的历史记录是否仍然存在,但是总有一天我可能需要脚本中的内容。
当我查看“ set”的输出时,似乎没有什么不同。谢谢!
答案 0 :(得分:1)
EDIT (更改代码)以列出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