Using WMI to get printer logs

1

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.

powershell
scripting
wmi
asked on Stack Overflow Mar 4, 2013 by Valrok • edited Mar 4, 2013 by alroc

1 Answer

0

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.

answered on Stack Overflow Mar 4, 2013 by alroc • edited Mar 5, 2013 by alroc

User contributions licensed under CC BY-SA 3.0