執行 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