Board logo

标题: [问题求助] 如何获取当前VBS执行的进程号 [打印本页]

作者: watson    时间: 2023-11-2 15:42     标题: 如何获取当前VBS执行的进程号

如何在代码中获取当前VBS代码执行的进程号。
作者: ShowCode    时间: 2023-11-2 16:15

回复 1# watson


1.vbs
  1. Set objShell = CreateObject("WScript.Shell")
  2. Set objScriptExec = objShell.Exec("tasklist /v /fo csv /fi ""IMAGENAME eq wscript.exe""")
  3. strScriptPID = objScriptExec.StdOut.ReadAll
  4. Set objRegEx = CreateObject("VBScript.RegExp")
  5. objRegEx.Global = True
  6. objRegEx.Pattern = "(\d+)"
  7. Set colMatches = objRegEx.Execute(strScriptPID)
  8. MsgBox strScriptPID
  9. If colMatches.Count > 0 Then
  10.     MsgBox colMatches(0).SubMatches(0)
  11. End If
复制代码

作者: ShowCode    时间: 2023-11-2 16:23

回复 1# watson


2.vbs
  1. Set oShell = CreateObject("WScript.Shell")
  2. Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  3. sCmd = "/k " & Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
  4. oShell.Run "%comspec% " & sCmd, 0
  5. Set oChldPrcs = oWMI.ExecQuery("Select * From Win32_Process Where CommandLine Like '%" & sCmd & "'",,32)
  6. For Each oCols In oChldPrcs
  7.     lOut = oCols.ParentProcessId
  8.     oCols.Terminate
  9.     Exit For
  10. Next
  11. MsgBox lOut
复制代码





欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2