Announcement

Collapse
No announcement yet.

VB Script to map printers based on computer group membership

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

  • VB Script to map printers based on computer group membership

    I am currently writing a script to map printers based on computer group membership. It seems to fail at line 40 (in bold in code below) and or just is not written correctly. Can someone please look at my code and help me. Thanks in advance!

    Code:
    Option Explicit
    Dim wshNetwork, ADSysInfo, CurrentComputer, strGroups, compPrinter, compPrinter1, objPrinter1, _
    objPrinter2, objPrinter3, objPrinter4, objPrinter5, objPrinter6
    ' Specify your print servers here. Note if you have more than one print server, you will need to modify
    ' the IF statements below. 
    compPrinter  = "printers"
    'compPrinter1 = ""
    ' Specify your printer share names here. If you have less than 5 comment out the ones not in use. To
    ' comment out a line enter a " ' " before the line.
    objPrinter1   = "Antigonish Branch - Printer 2"
    objPrinter2   = "Antigonish Branch - Printer 1"
    objPrinter3   = "Finance Department - Printer 4"
    objPrinter4   = "Corporate Office  Printer 3"
    objPrinter5   = "Teller - Antigonish Branch"
    objPrinter6         = "Check - Antigonish Branch"
    ' Constants for groups, enter in the names of the groups you have created. Again if you have less than 5
    ' comment those not in use out
    Const PRINTER_GROUP1   = "Finance"
    Const PRINTER_GROUP2   = "FSO Left"
    Const PRINTER_GROUP3    = "FSO Right"
    Const PRINTER_GROUP4   = "Tellers"
    Const PRINTER_GROUP5   = "Corporate Print"
    '=========================================================================================================
    Set wshNetwork = CreateObject("WScript.Network")
    Set ADSysInfo = CreateObject("ADSystemInfo")
    Set CurrentComputer = GetObject("LDAP://" & ADSysInfo.ComputerName)
    strGroups = LCase(Join(CurrentComputer.MemberOf))
    If InStr(1, strGroups, PRINTER_GROUP1, 1) Then
      wshNetwork.AddWindowsPrinterConnection _
     "\\" & compPrinter & "\" & objPrinter1
     "\\" & compPrinter & "\" & objPrinter2
     wshNetwork.SetDefaultPrinter _
     "\\" & compPrinter & "\" & objPrinter1
    End If
    If InStr(1, strGroups, PRINTER_GROUP2, 1) Then
         wshNetwork.AddWindowsPrinterConnection _
     "\\" & compPrinter & "\" & objPrinter2
     "\\" & compPrinter & "\" & objPrinter1
     wshNetwork.SetDefaultPrinter _
     "\\" & compPrinter & "\" & objPrinter2
    End If
    If InStr(1, strGroups, PRINTER_GROUP3, 1) Then
         wshNetwork.AddWindowsPrinterConnection _
     "\\" & compPrinter & "\" & objPrinter1
     "\\" & compPrinter & "\" & objPrinter2
     wshNetwork.SetDefaultPrinter _
     "\\" & compPrinter & "\" & objPrinter1
    End If
    If InStr(1, strGroups, PRINTER_GROUP4, 1) Then
         wshNetwork.AddWindowsPrinterConnection _
     "\\" & compPrinter & "\" & objPrinter5
     "\\" & compPrinter & "\" & objPrinter6
     "\\" & compPrinter & "\" & objPrinter1
     "\\" & compPrinter & "\" & objPrinter2
     wshNetwork.SetDefaultPrinter _
     "\\" & compPrinter & "\" & objPrinter5
    End If
    If InStr(1, strGroups, PRINTER_GROUP5, 1) Then
         wshNetwork.AddWindowsPrinterConnection _
     "\\" & compPrinter & "\" & objPrinter3
     "\\" & compPrinter & "\" & objPrinter1
     "\\" & compPrinter & "\" & objPrinter2
     wshNetwork.SetDefaultPrinter _
     "\\" & compPrinter & "\" & objPrinter3
    End If
    Last edited by Rems; 18th November 2011, 20:23.

  • #2
    Re: VB Script to map printers based on computer group membership

    Try,
    Code:
    <...>
    
    strGroups = Join(CurrentComputer.MemberOf)
    
    If InStr(1, strGroups, PRINTER_GROUP1, 1) Then
       wshNetwork.AddWindowsPrinterConnection _
         "\\" & compPrinter & "\" & objPrinter2
       wshNetwork.AddWindowsPrinterConnection _
         "\\" & compPrinter & "\" & objPrinter1
       wshNetwork.SetDefaultPrinter _
         "\\" & compPrinter & "\" & objPrinter1
    End If
    
    If InStr(1, strGroups, PRINTER_GROUP2, 1) Then
       wshNetwork.AddWindowsPrinterConnection _
         "\\" & compPrinter & "\" & objPrinter1
       wshNetwork.AddWindowsPrinterConnection _
         "\\" & compPrinter & "\" & objPrinter2
       wshNetwork.SetDefaultPrinter _
         "\\" & compPrinter & "\" & objPrinter2
    End If
    
    <...>
    Additionally,
    This strGroups = Join(CurrentComputer.MemberOf)
    will raise a "Type Mismatch" error on the Join function unless the memberOf attribute has at least two Distinguished Names.

    /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

    Working...
    X