进程完成时的 WMI 进程所有者
我有下面的代码,我试图收集有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需检查您的集合项是否仍包含有效的 WMI 对象引用。如果 WMI 对象不再存在,您的引用将变为 null。
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.