Announcement

Collapse
No announcement yet.

Help Needed: "GetDefaultPrinter" printer script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help Needed: "GetDefaultPrinter" printer script

    Hey gang. Looking for some help with my current printer script. I need to add some intelligence. I'd like the script to not force the network printer as default if the user has a local printer as their default.

    The script currently looks for the existence of any local printers. If it finds one, it does not force the network printer as default. But this is not "smart" enough. I don't care if my users have local printers. I care if they have a local printer set as their default.

    Here is my current script. Any help would be great!
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = TRUE")

    Set objNetwork = CreateObject("WScript.Network")
    ObjNetwork.AddWindowsPrinterConnection "\\IL1\Black and White Ricoh"
    ObjNetwork.AddWindowsPrinterConnection "\\IL1\COLOR Ricoh"

    If colPrinters.Count = 0 Then
    objNetwork.SetDefaultPrinter "\\IL1\Black and White Ricoh"
    End If

  • #2
    Re: Help Needed: "GetDefaultPrinter" printer script

    If you have a look at this script I'm sure you know how to figure out if the default printer is connected to a LPT port
    http://forums.petri.com/showthread.php?t=14014
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: Help Needed: "GetDefaultPrinter" printer script

      The easiest way is - doing the For Each objPrinter loop (from the collection of local printers), and get the name of the present default printer (if it is a local printer). This can best be done before adding any additional printers.
      To enumerate local printers only, use this statement: Network IsNot True (instead of just Local = TRUE , because if both the Local and Network properties are set to TRUE, then the printer is a network printer!!)

      Code:
      ' Get the current default printer if it is not a network printer. 
      strComputer = "."
      Set objWMIService = GetObject( _
          "winmgmts:{impersonationLevel=impersonate}!\\" _
          & strComputer & "\root\cimv2")
      Set colInstalledPrinters = objWMIService.ExecQuery _
          ("Select * from Win32_Printer Where Network != TRUE")
      For Each objPrinter in colInstalledPrinters
            If objPrinter.Default = TRUE then
              sDefaultPrinter = objPrinter.Name
            End If
      Next
      
      On Error Resume next
      
      ' Add the network printers
      Set objNetwork = CreateObject("WScript.Network")
      sNwDefaultPrinter = "\\IL1\Black and White Ricoh"
      ObjNetwork.AddWindowsPrinterConnection sNwDefaultPrinter
      ObjNetwork.AddWindowsPrinterConnection "\\IL1\COLOR Ricoh"
      
      ' Setting the correct Default printer
      If len(sDefaultPrinter) = 0 _
        Then sDefaultPrinter = sNwDefaultPrinter
      objNetwork.SetDefaultPrinter sDefaultPrinter
      \Rems
      Last edited by Rems; 29th July 2008, 23:09.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Help Needed: "GetDefaultPrinter" printer script

        Rems - You are a life saver. Thank you. Worked great.

        Comment

        Working...
        X