One Step Further: Working with Printers


In this exercise, we will explore the use of WMI to obtain information about printers defined on your machine. As we do this, we will examine the use of several Windows PowerShell cmdlets that can dramatically simplify the use of WMI in our scripting.

  1. Start Windows PowerShell by using Start | Run | PowerShell.

  2. Open Notepad.exe or your favorite script editor.

  3. Open the image from book WmiTemplate.ps1 file from the scripts folder for this chapter and save it as yournameimage from book WorkingWithPrinters.ps1. Refer to the image from book WorkingWithPrinters.ps1 script already in this chapter’s script folder to see a sample of a completed solution to this exercise.

  4. Modify the $wmiQuery line to query the WIN32_Printer class. The revised line is shown here:

     $wmiQuery = "Select * from win32_Printer"

  5. Save and run the script. Your output will be similar to the following partial output:

     Status                      : Unknown Name                        : Lexmark 4039 Plus PS __GENUS                     : 2 __CLASS                     : Win32_Printer __SUPERCLASS                : CIM_Printer __DYNASTY                   : CIM_ManagedSystemElement __RELPATH                   : Win32_Printer.Device __PROPERTY_COUNT            : 86 __DERIVATION                : {CIM_Printer, CIM_LogicalDevice, CIM_LogicalEleme                               nt, CIM_ManagedSystemElement} __SERVER                    : MRED1 __NAMESPACE                 : root\cimv2 __PATH                      : \\MRED1\root\cimv2:Win32_Printer.Devicelistitem"> 

    Change the script so that it only prints out the name of each printer. To do this, add the name property to the Format-List cmdlet, as shown here:

     $objWMIServices | Format-List name

  6. Save and run your script. You will see an output similar to the following:

     name : Microsoft XPS Document Writer name : Lexmark 4039 Plus PS

  7. To determine where each printer is defined, add the portname property to the Format-List cmdlet. This is shown here:

     $objWMIServices | Format-List name, portname

  8. Save and run the script. The resulting output will be similar to the one shown here:

     name     : Microsoft XPS Document Writer portname : XPSPort: name     : Lexmark 4039 Plus PS portname : IP_192.168.1.89

  9. To identify the capabilities of each printer, add the capabilitydescriptions property to the output. To do this, revise the Format-List line, as shown here:

     $objWMIServices | Format-List name, portname, capabilitydescriptions

  10. The resulting output is shown here:

     name                   : Microsoft XPS Document Writer portname               : XPSPort: capabilitydescriptions : {Copies, Color, Duplex, Collate} name                   : Lexmark 4039 Plus PS portname               : IP_192.168.1.89 capabilitydescriptions : {Copies, Color, Duplex, Collate}

  11. To determine whether there are other properties you may be interested in using from the WIN32_Printer WMI class, you can pipeline the object contained in the $objWmiServices variable to the Get-Member cmdlet. To reduce typing, use the gm alias, as shown here:

     $objWMIServices | GM

  12. If you find additional interesting properties, add them to the Format-List line as we did with the name, portname, and capabilitydescriptions properties. Some you may wish to investigate include driverName, ServerName, HorizontalResolution, and Vertical-Resolution.

  13. This concludes this one step further exercise.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net