进程完成时的 WMI 进程所有者

发布于 2024-12-23 18:40:46 字数 2711 浏览 1 评论 0原文

我有下面的代码,我试图收集有关 Windows Server 2003 中进程的丰富信息。当我试图拉回在初始 WMI 查询和获取 WMI 调用之间完成的进程的进程所有者时,就会出现问题。所有者。如果该过程同时完成,则会引发错误并且脚本停止。我宁愿只检查该进程是否不可用,如果不可用则不输出到控制台。我查看了参考资料,似乎没有合适的方法。非常感谢任何帮助。

引发的错误是...

getProcessInfo2.vbs(42, 5) SWbemObjectEx: 未找到

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

I have the following code where I am trying to gather rich information about processes in Windows Server 2003. My problem occurs when I'm trying to pull back the process owner for a process which finishes between the initial WMI query and the call to get the owner. If the process has finished in the mean time then an error is raised and the script stops. I'd rather just check if the process is not available and not output to the console if it isn't. I've looked at the reference and there doesn't seem to be an appropriate method for this. Any help much appreciated.

The error raised is...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-12-30 18:40:46

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

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

You just need to check whether your collection item still contains a valid WMI object reference. If the WMI object no longer exists, your reference will become null.

For Each objItem in colItems
    If IsObject(objItem) Then
        ...
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文