Announcement

Collapse
No announcement yet.

Adding printers and setting default

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

  • Adding printers and setting default

    I have managed to steal, borrow and add some other borrowed code to make the following script. When just the add printer section is run, the printers are added but not all the existing printers are deleted and I do not understand why.

    When the Set Default printer section is included, it errors on the first line and that is as far as it goes. The actual computer names are Room0101, Room0102, Room0103 or Room1101, Room1102, Room1103 etc.

    Any ideas on where the stuffup is would be greatly appreciated.

    Code:
    '---------------------------------------------------------------------------------
    'Project Name: Printer set
    'Aim: Install Printers then set default printer based on machine name
    'Date: 070821
    'Rev: 1.0
    
    '----------------------------------------------------------------------------------
    On Error Resume Next 
    
    '-----------------------------------------------------------------------------------
    'create object
    Set WshNetwork = CreateObject("WScript.Network")
    
    '-----------------------------------------------------------------------------------
    'remove existing printers
    Set clrPrinters = WshNetwork.EnumPrinterConnections 
    For j = 0 to clrPrinters.Count -1 Step 2 
        WshNetwork.RemovePrinterConnection clrPrinters.Item(j+1), true, true 
    Next 
    
    'install all Curriculum network printers
    
    'Room 9
    PrinterPath = "\\e1345fs004\curric1"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Computer Lab
    PrinterPath = "\\e1345fs004\curric2"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 11 - 12
    PrinterPath = "\\e1345fs004\curric3"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 3 - 4
    PrinterPath = "\\e1345fs004\curric4"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 1 - 2
    PrinterPath = "\\e1345fs004\curric5"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 5 - 6
    PrinterPath = "\\e1345fs004\curric6"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 20 - 21
    PrinterPath = "\\e1345fs004\curric7"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 13 - 14
    PrinterPath = "\\e1345fs004\curric8"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 15 - 16
    PrinterPath = "\\e1345fs004\curric9"
    PrinterDriver = "Lexmark T522"
    WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Room 17 - 18
    'PrinterPath = "\\e1345fs004\curric10"
    'PrinterDriver = "Lexmark T522"
    'WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    'Computer Lab - Colour
    'PrinterPath = "\\e1345fs004\curric11"
    'PrinterDriver = "Lexmark C710"
    'WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
    
    
    'Following line is for testing
    'Msgbox "Adding printers complete"
    
    '---------------------------------------------------------------------------------
    
    'get room number from computer name
    
    ComputerName = WSHNetwork.ComputerName
    Mystring = Left(Computername, 6)
    
    'the following two lines are for testing purpose
    'Msgbox "Computer name is" + Computername
    'Msgbox "Strip Number is" + Mystring
    
    '---------------------------------------------------------------------------------
    
    'set default printer based on room number
    
    'rem Select Case Mystring 
    
    'Classroom and Lab machines
    
    
    'Case "Room01"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric5"
    
    'Case "Room02"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric5"
    
    'Case "Room03"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric4"
    
    'Case "Room04"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric4"
    
    'Case "Room05"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric6"
    
    'Case "Room06"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric6"
    
    'Case "Room08"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric1"
    
    'Case "Room09"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric1"
    
    'Case "Room11"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric3"
    
    'Case "Room12"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric3"
    
    'Case "Room13"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric3"
    
    'Case "Room14"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric3"
    
    'Case "Room15"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric9"
    
    'Case "Room16"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric9"
    
    'Case "Room17"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric9"
    
    'Case "Room18"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric9"
    
    'Case "Room20"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric7"
    
    'Case "Room21"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric7"
    
    'Case "Lab"
    WshNetwork.SetDefaultPrinter "\\e1345fs004\curric2"
    
    
    
    '======================================================
    
    'end of script
    '------------------------------------------------------------------------------------
    1 1 was a racehorse.
    2 2 was 1 2.
    1 1 1 1 race 1 day,
    2 2 1 1 2

  • #2
    Re: Adding printers and setting default

    For deleting printers, you can also use WMI service.

    "The RemovePrinterConnection method removes both Windows and MS-DOS-based printer connections.
    If the printer was connected using AddPrinterConnection, the printerName argument must be the same as the printers local port.
    If the printer was set up using the AddWindowsPrinterConnection method or was added manually through the Add Printer wizard, the printerName argument must be the printers UNC name."

    Win32_Printer class
    http://msdn2.microsoft.com/en-us/library/aa394363.aspx
    The WMI class Win32_Printer can distinguish between local printers and network printers (Network = FALSE or Where Network = TRUE), but it also supports the Delete_ method.
    Code:
    strComputer = "."
    Set objWMIService = GetObject( _
        "winmgmts://" & strComputer & "\root\cimv2")
    Set colInstalledPrinters =  objWMIService.ExecQuery _
        ("Select * from Win32_Printer")
    For Each objPrinter in colInstalledPrinters
         objPrinter.Delete_
    Next

    About the Set Default printer section,
    There is a line missing at the end of your script
    End Select
    Code:
    ' <...>
    
    'get room number from computer name
    
    ComputerName = UCase(WSHNetwork.ComputerName)
    
    ' note, because of UCase(), the Computer name string is in uppercase
    ' so the Roomsnn must also be fully in upper-case, else there is no match. 
    
    'set default printer based on room number
    '  Classroom and Lab machines
    Select Case Left(ComputerName, 6)
    
    Case "ROOM01", "ROOM02"
       WshNetwork.SetDefaultPrinter "\\e1345fs004\curric5"
    
    Case "ROOM03", "ROOM04"
       WshNetwork.SetDefaultPrinter "\\e1345fs004\curric4"
    
    Case "ROOM05", ROOM06
       WshNetwork.SetDefaultPrinter "\\e1345fs004\curric6"
    
    Case "ROOM08", "ROOM09"
       WshNetwork.SetDefaultPrinter "\\e1345fs004\curric1"
    
    Case "ROOM11", "ROOM12", "ROOM13", "ROOM14"
       WshNetwork.SetDefaultPrinter "\\e1345fs004\curric3"
    
    ' <...>
    End Select
    See also Tom's answer to this thread;
    http://forums.petri.com/showthread.php?t=18120
    it about using Select Case

    \Rems

    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


    • #3
      Re: Adding printers and setting default

      Thanks Rems. Is there any work around for the computer names? The way I had read my info was that:

      Code:
      Case "Room01"
         WshNetwork.SetDefaultPrinter "\\e1345fs004\curric5"
      as I had originally set it, was not case sensitive to the computer name.
      Is there anything to use so I don't have to go and change all the names of every machine on site and the case of the machine name is made irrelevant?

      Next rollout or reimage, the bloody machine name will be one case or the other.
      1 1 was a racehorse.
      2 2 was 1 2.
      1 1 1 1 race 1 day,
      2 2 1 1 2

      Comment


      • #4
        Re: Adding printers and setting default

        Code:
        'remove existing printers
        Set clrPrinters = WshNetwork.EnumPrinterConnections 
        For j = 0 to clrPrinters.Count -1 Step 2 
            WshNetwork.RemovePrinterConnection clrPrinters.Item(j+1), true, true 
        Next
        Any reason for the STEP 2 in line 3 above?
        This would remove every other printer, wouldn't it?
        Tom Jones
        MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
        PhD, MSc, FIAP, MIITT
        IT Trainer / Consultant
        Ossian Ltd
        Scotland

        ** Remember to give credit where credit is due and leave reputation points where appropriate **

        Comment


        • #5
          Re: Adding printers and setting default

          I have no idea. All the code was stolen or borrowed from other working scripts. I have just tried to throw it together to make it work. I understand less than 1% of it. Seriously!!
          1 1 was a racehorse.
          2 2 was 1 2.
          1 1 1 1 race 1 day,
          2 2 1 1 2

          Comment


          • #6
            Re: Adding printers and setting default

            If it isnt deleting all the printers, try removing the STEP 2 and see how that works
            Tom Jones
            MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
            PhD, MSc, FIAP, MIITT
            IT Trainer / Consultant
            Ossian Ltd
            Scotland

            ** Remember to give credit where credit is due and leave reputation points where appropriate **

            Comment


            • #7
              Re: Adding printers and setting default

              Ok, thanks, I shall try that once I get the server back up and running. Replace some RAM and get a Parity ErrorSystem Halted. Great!! Service call logged.
              1 1 was a racehorse.
              2 2 was 1 2.
              1 1 1 1 race 1 day,
              2 2 1 1 2

              Comment


              • #8
                Re: Adding printers and setting default

                http://msdn2.microsoft.com/en-us/lib...ections Method
                Without the 'step 2', the loop shows the port first then the next round it shows the name of the printer, so every printer is passed twice by default. (simply because the array created by EnumPrinterConnections is filled like that)
                To avoid that you have to use 'step 2'.

                The minus 1 before 'step 2' is because the printercount was started from 1 but the loop-count starts with 0 because the first value of an array is at 0. Without the -1 the script will end in an error because the loop is out of the present range.

                Because the loop starts from 0 it now will only show the even numbers and that happen to be the 'port'. That is why in the loop (Loop_count + 1) is used to get the odd number of the same printer, that will be the fullname.

                This script below give the same results and may seem a bit more logic,
                but if you know you are dealing with an array (and arrays always start counting from 0) it is not feeling right to skip a first value and is now even less logic to use only the 'odd rounds'.
                Code:
                For j = 1 to clrPrinters.Count 
                    If CBool(ABS(j) mod 2) then _
                    WshNetwork.RemovePrinterConnection clrPrinters.Item(j), true, true 
                Next


                "Is there anything to use so I don't have to go and change all the names of every machine on site"
                In the example I use the function UCASE(Computername), that sets the string in upper-case (you can also use Lcase() instead, that would have set all the characters in lower-case for the script). This script function can be used to avoid problems concerning case diffences in the original values if you want to compare them with another string. UCase("Room0101") will be read by the script as ROOM0101, you don't have to change all computernames on site. This function may not seem nessesary at the moment if the comparison works fine as it is now, but using it anyway can avoid problems when nieuw computeraccount are created in the future.


                "I shall try that once I get the server back up and running"
                Oh oh, good luck with that.

                \Rems
                Last edited by Rems; 1st September 2007, 22:20.

                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


                • #9
                  Re: Adding printers and setting default

                  strComputer = "."
                  Set objWMIService = GetObject( _
                  "winmgmts://" & strComputer & "\root\cimv2")
                  Set colInstalledPrinters = objWMIService.ExecQuery _
                  ("Select * from Win32_Printer")
                  For Each objPrinter in colInstalledPrinters
                  objPrinter.Delete_
                  Next
                  Thanks Rems, your above code did the trick and remove all the stuborn printers. Just have a to make sure all the computer names are correct now as some idiots have gone and move machines and not told me.

                  Sidenote: Left the server down overnight and then reinstalled the RAM. Worked a treat. Did some clean up work for about 30 minutes and then stood up to leave. Blasted server started beeping very loudly......HDD failure in one of the RAID channels. Not a really great weekend that one.

                  Again, many thanks for your work on repairing my "acquired" code.
                  1 1 was a racehorse.
                  2 2 was 1 2.
                  1 1 1 1 race 1 day,
                  2 2 1 1 2

                  Comment

                  Working...
                  X