开始时间、运行持续时间和;在Windows中通过PID定位应用程序

开始时间、运行持续时间和;在Windows中通过PID定位应用程序,windows,batch-file,vbscript,cmd,Windows,Batch File,Vbscript,Cmd,是否有任何方法可以使用CMD或VBS在windows下使用PID值获取运行应用程序的开始时间(带日期)、总运行时间和位置?如果是,怎么做?提前感谢。并非如此(时间路径可用) 您可以监视进程的启动和退出,并自己计算 因此,Tasklist/v提供了包含路径的命令行 在VBS中也是如此 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colItems = ob

是否有任何方法可以使用CMD或VBS在windows下使用PID值获取运行应用程序的开始时间(带日期)、总运行时间和位置?如果是,怎么做?提前感谢。

并非如此(时间路径可用)

您可以监视进程的启动和退出,并自己计算

因此,
Tasklist/v
提供了包含路径的命令行

在VBS中也是如此

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    msgbox objItem.ProcessID & " " & objItem.Caption
Next
这是一个VBS脚本,用于监视进程的启动和退出

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
Loop
没有StartTime这样的东西

class Win32_Process : CIM_Process
{
  string Caption;
  string CommandLine;
  string CreationClassName;
  datetime CreationDate;
  string CSCreationClassName;
  string CSName;
  string Description;
  string ExecutablePath;
  uint16 ExecutionState;
  string Handle;
  uint32 HandleCount;
  datetime InstallDate;
  uint64 KernelModeTime;
  uint32 MaximumWorkingSetSize;
  uint32 MinimumWorkingSetSize;
  string Name;
  string OSCreationClassName;
  string OSName;
  uint64 OtherOperationCount;
  uint64 OtherTransferCount;
  uint32 PageFaults;
  uint32 PageFileUsage;
  uint32 ParentProcessId;
  uint32 PeakPageFileUsage;
  uint64 PeakVirtualSize;
  uint32 PeakWorkingSetSize;
  uint32 Priority;
  uint64 PrivatePageCount;
  uint32 ProcessId;
  uint32 QuotaNonPagedPoolUsage;
  uint32 QuotaPagedPoolUsage;
  uint32 QuotaPeakNonPagedPoolUsage;
  uint32 QuotaPeakPagedPoolUsage;
  uint64 ReadOperationCount;
  uint64 ReadTransferCount;
  uint32 SessionId;
  string Status;
  datetime TerminationDate;
  uint32 ThreadCount;
  uint64 UserModeTime;
  uint64 VirtualSize;
  string WindowsVersion;
  uint64 WorkingSetSize;
  uint64 WriteOperationCount;
  uint64 WriteTransferCount;
};
您可以使用VBScript查询WMI类,以获取进程的可执行路径和开始时间。持续时间可以从开始时间派生:

pid = 23

Set wmi = GetObject("winmgmts://./root/cimv2")
Set convert = CreateObject("WbemScripting.SWbemDateTime")

qry = "SELECT * FROM Win32_Process WHERE ProcessId = " & pid
For Each p In wmi.ExecQuery(qry)
  If IsNull(p.CreationDate) Then
    'leave start time and duration empty if CreationDate can't be read
    startTime = ""
    duration  = ""
  Else
    'convert start time from a string yyyyMMddHHmmss.ffffff±zzz to a date
    convert.Value = p.CreationDate
    startTime = convert.GetVarDate(True)

    'calculate duration in minutes
    duration  = DateDiff("n", startTime, Now)
  End If
  WScript.Echo startTime & vbTab & duration & vbTab & p.ExecutablePath
Next

请注意,您需要
SeDebugPrivilege
权限(管理员默认拥有该权限)才能查看其他用户进程的可执行路径。如果没有该权限
p.ExecutablePath
Null
用于未在当前用户上下文中运行的进程。

未获取路径。。。。这是
Tasklist/v
的输出,很抱歉弄混了<代码>wmic进程获取/格式:hform>output.htm或
htable
。使用IE查看文件。此
wmic进程get/format:htable
显示运行应用程序位置的命令,而不像
nginx
显示
nginx
apache
如果显示
K:\portable\u server\apache\bin\httpd.exe-dk:/portable\u server/apache
php cgi
显示
K:/portable\u server/languages/php/php-5.6.4-Win32-VC11-x86/php-cgi.exe-b admin:9000-c K:/portable\u server/bin/php cgi.ini
这不是我想要的。我想,对于
nginx
output
K:\portable\u-server\nginx\nginx.exe
for
apache
K:\portable\u-server\apache\bin\httpd.exe和
php-cgi
output
K:\portable\u-server\languages\php\php-5.6.4-Win32-VC11-x86\php.exe
大多数程序启动都指定一个完整的路径,比如explorer(通常)和CMD。如果您有自定义启动器,API调用CreateProcess将搜索它。这在正常操作中很少见。我不知道您的程序是什么服务。请参阅
sc qc servicename
wmic service get pid
返回错误
K:\lab>pid=11896'pid'未被识别为内部或外部命令,o可运行的程序或批处理文件。K:\lab>Set wmi=GetObject(“winmgmts://./root/cimv2)K:\lab>Set convert=CreateObject(“WbemScripting.SWbemDateTime”)K:\lab>qry=“从Win32_进程中选择*,其中ProcessId=”&pid“qry”未被识别为内部或外部命令、可操作程序或批处理文件。“pid”未被识别为内部或外部命令、可操作程序或批处理文件。此时每个都是意外的。对于wmi.ExecQuery(qry)中的每个p,K:\lab>
@AlMasumNishat为什么要在命令提示符下粘贴VBScript代码?将其写入.vbs文件,并使用
wscript.exe
cscript.exe运行该文件。