流程结束时的WMI流程所有者

时间:2011-12-29 23:57:35

标签: process vbscript wmi

我有以下代码,我正在尝试收集有关Windows Server 2003中的进程的丰富信息。当我尝试撤回进程所有者以完成初始WMI查询和调用之间的进程时出现问题得到主人。如果进程在平均时间内完成,则会引发错误并停止脚本。我宁愿只检查进程是否不可用,如果不是,则不输出到控制台。我查看了参考文献,似乎没有适当的方法。任何帮助非常感谢。

引发的错误是......

getProcessInfo2.vbs(42, 5) SWbemObjectEx: Not found

strComputer = "." 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"_
     & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Process",,48) 

For Each objItem in colItems 

    'Calculated Fields
    sngProcessTime = ( CSng(objItem.KernelModeTime) + CSng(objItem.UserModeTime)) / 10000000


    Wscript.Echo
    Wscript.Echo "ProcessId                  : " & objItem.ProcessId
    Wscript.Echo "CommandLine                : " & objItem.CommandLine
    Wscript.Echo "CreationDate               : " & objItem.CreationDate
    Wscript.Echo "HandleCount                : " & objItem.HandleCount
    Wscript.Echo "Name                       : " & objItem.Name
    Wscript.Echo "PageFaults                 : " & objItem.PageFaults
    Wscript.Echo "PageFileUsage              : " & objItem.PageFileUsage
    Wscript.Echo "ParentProcessId            : " & objItem.ParentProcessId
    Wscript.Echo "PeakPageFileUsage          : " & objItem.PeakPageFileUsage
    Wscript.Echo "PeakVirtualSize            : " & objItem.PeakVirtualSize
    Wscript.Echo "PeakWorkingSetSize         : " & objItem.PeakWorkingSetSize
    Wscript.Echo "Priority                   : " & objItem.Priority
    Wscript.Echo "QuotaNonPagedPoolUsage     : " & objItem.QuotaNonPagedPoolUsage
    Wscript.Echo "QuotaPagedPoolUsage        : " & objItem.QuotaPagedPoolUsage
    Wscript.Echo "QuotaPeakNonPagedPoolUsage : " & objItem.QuotaPeakNonPagedPoolUsage
    Wscript.Echo "QuotaPeakPagedPoolUsage    : " & objItem.QuotaPeakPagedPoolUsage
    Wscript.Echo "ReadOperationCount         : " & objItem.ReadOperationCount
    Wscript.Echo "ReadTransferCount          : " & objItem.ReadTransferCount
    Wscript.Echo "ThreadCount                : " & objItem.ThreadCount
    Wscript.Echo "VirtualSize                : " & objItem.VirtualSize
    Wscript.Echo "WriteOperationCount        : " & objItem.WriteOperationCount
    Wscript.Echo "WriteTransferCount         : " & objItem.WriteTransferCount
    Wscript.Echo "CPUTime                    : " & sngProcessTime

    Dim Return
    Return = objItem.GetOwner(strNameOfUser)
    If Return <> 0 Then strNameOfUser = "unavailable"

    Wscript.Echo "Owner                      : " & strNameOfUser    
Next

1 个答案:

答案 0 :(得分:1)

您只需要检查您的集合项是否仍包含有效的WMI对象引用。如果WMI对象不再存在,则引用将变为null。

For Each objItem in colItems
    If IsObject(objItem) Then
        ...
    End If
Next