Get-Printer question

This topic contains 6 replies, has 3 voices, and was last updated by  Phil L 1 month, 1 week ago.

  • Author
    Posts
  • #89186

    Phil L
    Participant

    Hi All,
    I am relatively new to Powershell. I am trying to write a script that will give me all printers with errors in their print queues. In addition I need to pull them from multiple servers. So far I have the following:

    get-printer -computername "servername1", "servername2", "servername3"
    | where printerstatus -eq Error | Format-list Name, Jobcount

    I keep getting an error related to the -computername parameter. No matter what I try with multiple server entries it does not work. It DOES work with a single server entry.

    Any help is appreciated!

  • #89189

    John Steele
    Participant

    Running 'Get-Help Get-Printer -Full' will show you that the ComputerName parameter only accepts one string.
    You will need to create a foreach loop and pass the computers in one at a time.

    $computerName = "servername1", "servername2", "servername3"
    
    foreach ($computer in $computerName) {
    
        Get-Printer -ComputerName $computer | where printerstatus -eq Error | Format-list Name, Jobcount
    }
    
    • #89195

      Phil L
      Participant

      Thank You MUCH John! I notice there is no object for servername with Get-Printer. Do you know of a way to pull in the servername for each printer's output?

    • #89197

      John Steele
      Participant

      Using your example, if you are just looking for the computer name to be part of the output you just need to add the ComputerName property to Format-List.

      $computerName = "servername1", "servername2", "servername3"
      
      foreach ($computer in $computerName) {
      
          Get-Printer -ComputerName $computer | where printerstatus -eq Error | Format-list ComputerName, Name, Jobcount
      }
      
    • #89198

      Phil L
      Participant

      Thank You! Very much Appreciate it John! Enjoying learning Powershell and am extremely grateful for the support of the community!

  • #89233

    Richard Siddaway
    Moderator

    Get-Printer can accept a collection of CimSessions so you could do something like

    $cs = New-CimSession -ComputerName "servername1", "servername2", "servername3"
    Get-Printer -CimSession $cs

    You could even possibly do

    Get-Printer -CimSession "servername1", "servername2", "servername3"

    As CimSession will often take list of computer names and automatically create and dispose of the sessions

    • #89267

      Phil L
      Participant

      Thanks Richard!

You must be logged in to reply to this topic.