使用vbscript查看正在运行的进程

时间:2017-05-20 02:50:39

标签: vbscript process

我想看到我的计算机上运行的所有进程,但cmd命令只提供应用程序,而不是任何脚本或更小的文件。我试图找出一种方法,以更高级的方式列出所有进程,列出当前正在运行的一切。有没有人知道用vbscript做到这一点的方法?或者,如果有更好的方法来做到这一点是什么?

2 个答案:

答案 0 :(得分:2)

使用TaskList命令

TaskList命令可用于显示所有正在运行的应用程序和服务的列表及其详细信息和进程ID(PID)。

Dim ProTFPath, ProTF, StrPrInfo, StrPrInfoA, PrInfo

Set WshShell = WScript.CreateObject("Wscript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

ProTFPath = "C:\PROCESSES.txt"

WshShell.Run "CMD /C TASKLIST /V /FO LIST > """ + ProTFPath + """", 0, True
' Here Run is used instead Exec to avoid console window flashes.

If FSO.FileExists(ProTFPath) Then
  Set ProTF = FSO.OpenTextFile(ProTFPath, 1, False)
End If

StrPrInfoA = ProTF.ReadAll

PrInfo = Split(StrPrInfoA, VbCrLf + VbCrLf)

For I = 0 To UBound(PrInfo)
  WScript.Echo PrInfo(I)
Next

Erase PrInfo
ProTF.Close

如果您不再需要此文件,请在脚本末尾添加以下行:

If FSO.FileExists(ProTFPath) Then
  FSO.DeleteFile(ProTFPath, True)
End If

查看有关TaskList here.

的更多信息

答案 1 :(得分:0)

EXE_Process = AllProcessRunningEXE(".")
Vbs_Process = AllProcessRunningVBS (".")

Function AllProcessRunningEXE( strComputerArg )
strProcessArr = ""
    Dim Process, strObject
    strObject   = "winmgmts://" & strComputerArg
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
        strProcessArr = strProcessArr & ";" & vbNewLine & Process.name
    Next
    AllProcessRunningEXE = Mid(strProcessArr,3,Len(strProcessArr))
End Function


Function AllProcessRunningVBS (strComputerArg)
    strProcessArr = ""
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
    For Each objItem in colItems
        strProcessArr = strProcessArr & ";" & vbNewLine & objItem.CommandLine
    Next
    AllProcessRunningVBS = Mid(strProcessArr,3,Len(strProcessArr))

    Set objWMIService = Nothing
    Set colItems = Nothing
End Function