使用 VBS 查找进程 ID

Find Process ID with VBS

在下面的VB脚本中,Excel个文件被打开。另外,我还必须找出在创建单个 Excel 文件时分配给它们的进程 ID。喜欢...

'Start VBS-FILE
WScript.Echo 'Process ID from VBS-File

'Open EXCEL-FILE
SET objExcel = CreateObject("Excel.Application")
objExcel.Visible = True: SET ObjWorkbook = objExcel.Workbooks.Add()
WScript.Echo 'Process ID from EXCEL OBJECT
'hide the cmd windows from popup
If StrComp(right(WScript.FullName,11),"wscript.exe",1) = 0 Then    
  WScript.Quit CreateObject("WScript.Shell").Run("cscript.exe """ & WScript.ScriptFullName & """", 0, False)
End If

Dim sh : Set sh = CreateObject("WScript.Shell")
'Start VBS-FILE  the full path for script "C:\Users\ENG\MyScriptFile.vbs"
Set Rtn = sh.Exec("wscript.exe C:\Users\ENG\MyScriptFile.vbs")
WScript.Sleep 1000  'stop script 1 sec for waiting OPEN the script
MsgBox "my script file PID  : " & vbTab & Rtn.ProcessID 'Process ID for SCRIPT FILE

'Open EXCEL-FILE New Excel file
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True : Set ObjWorkbook = objExcel.Workbooks.Add()

Set Rtn = sh.Exec("cmd.exe /c tasklist /v /fi ""imagename EQ excel*"" /FO LIST | FIND ""PID:""")
WScript.Sleep 1000   'stop script 1 sec for waiting execute cmd line
MsgBox "my EXCEL FILE " & Rtn.StdOut.ReadLine   'Process ID for EXCEL File