执行 WMI 方法
某些 WMI 类公开允许你对该对象执行某些操作的方法。例如, Win32_Printer 类有 11 种与打印机交互的方法,其中一种是 PrintTestPage 方法。以下代码演示了如何选择特定打印机并打印测试页。
'Specify the name of the target computer
strComputer = "."
'Note: Backslash is a special character that must be escaped with a backslash
'This means the UNC \\Network\Path\PrinterName must be written like the following
strQuery = "SELECT * FROM Win32_Printer WHERE DeviceID='\\\\Network\\Path\\PrinterName'"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\ROOT\cimv2")
Set colItems = objWMIService.ExecQuery(strQuery)
'ExecQuery returns a collection object, even when there's only 1 item in the collection
For Each objItem In colItems
'The PrintTestPage method takes no parameters and returns a UINT32
intTestPageReturnCode = objItem.PrintTestPage
Next
'PrintTestPage will return 0 (Successs) or 5 (Failure)
Select Case intTestPageReturnCode
Case 0
WScript.StdOut.WriteLine "Test page successfully printed"
Case 5
WScript.StdOut.WriteLine "Test page printing failed"
Case Else
WScript.StdOut.WriteLine "An unknown error occurred while printing a test page"
End Select