使用 WMI 获取打印机日志 [英] Using WMI to get printer logs

查看:47
本文介绍了使用 WMI 获取打印机日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WMI 从多个服务器获取打印机系统日志.一周前,我制作了以下代码,但由于某些原因,有时只能使用:

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

我想知道是否有人知道为什么会发生这种情况,以及是否有更好的方法来重写我的代码.我已经考虑使用 get-wmiobject cmdlet,但是我不确定如何过滤和获取我想要获取的相同日志.

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.

推荐答案

有两种方法可以做到这一点.都不使用 Get-WMIObject.

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

选项 1:获取整个事件日志,然后过滤.

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;

选项 2:从源头过滤

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

最佳做法是尽可能靠近数据源进行过滤(左过滤,右格式化),在本例中为选项 2.

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.

这篇关于使用 WMI 获取打印机日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆