Using WMI to get printer logs

夙愿已清 提交于 2020-01-05 05:34:11

问题


I'm trying to use WMI to get printer system logs from several servers. A week ago I made the following code which for some reason only works sometimes:

wmic /node:<servername> NTEvent WHERE "logfile='System' AND SourceName='Print' AND TimeGenerated > '20130219'" get EventCode,TimeGenerated,Message 

This line of code sometimes will work, but the majority of the time I'm getting the following error whenever I've tried running it to get logs:

ERROR:
Code = 0x80020009
Description = Exception occurred.
Facility = Dispatch

I was wondering if anyone may know why this is occurring and if there would be a better method to rewrite my code. I've considered using the get-wmiobject cmdlet, however I'm not sure how to filter and get the same logs that I'm trying to get.


回答1:


There are two ways to do this. Neither uses Get-WMIObject.

Option 1: Get the whole event log, then filter.

Get-EventLog -LogName System -Source Print|where-object{$_.timeGenerated -gt (get-date "2013-02-19")}|select-object eventid, timegenerated,message | Export-csv -path r:\log.csv -notypeinfo;

Option 2: Filter at the source

Get-WinEvent -FilterHashtable @{logname='system';source='print';StartTime=(get-date "2013-02-19").date;}|select-object id,timecreated,message;

Best practice is to filter as close to the source of the data as possible (Filter Left, Format Right), which would be option 2 in this case.



来源:https://stackoverflow.com/questions/15210764/using-wmi-to-get-printer-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!