Announcement

Collapse
No announcement yet.

Script to Map Printers based on IP address of client

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

  • Script to Map Printers based on IP address of client

    Hi All,

    I'm looking for a script that will map printers based on the clients IP address. I have a list of printer UNC paths and the subnets that they belong to. What I would like the script to do is check the clients IP address and if it falls into a certain range then map the appropriate printer E.g

    Subnet 1 10.10.1.x /24 - \\server01\printer01
    Subnet 2 10.10.2.x /24 - \\server01\printer02
    Subnet 3 10.10.2.x /24 - \\server01\printer03
    Subnet 4 10.10.2.x /24 - \\server01\printer04

    I found the following but it looks like a .bat file that is obtaining the IP address, not the VB script:

    http://forums.petri.com/archive/index.php/t-7759.html

    Thanks

    Michael
    Michael Armstrong
    www.m80arm.co.uk
    MCITP: EA, MCTS, MCSE 2003, MCSA 2003: Messaging, CCA, VCP 3.5, 4, 5, VCAP5-DCD, VCAP5-DCA, ITIL, MCP, PGP Certified Technician

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

  • #2
    Re: Script to Map Printers based on IP address of client

    Have you got any AD sites configured with those subnets? If so the easiest way IMO would be to link the GPO with a printer mapping script to the Site.
    Otherwise to get you started, you could probably adapt Select Case strSubnet as in the following:

    Code:
     
    'Assign printers to laptop users
    Select Case strSubnet
        Case "192.168.1.0"
            WSHNetwork.AddWindowsPrinterConnection "\\Server\Printer1"
            
        Case "192.168.2.0"
            WSHNetwork.AddWindowsPrinterConnection "\\Server2\Printer1"
            
    End Select
    
    WScript.Quit
    Ref: http://www.tek-tips.com/viewthread.c...1002848&page=8
    Caesar's cipher - 3

    ZKHQ BRX HYHQWXDOOB GHFLSKHU WKLV BRX ZLOO UHDOLVH LW ZDV D ZDVWH RI WLPH!

    SFX JNRS FC U6 MNGR

    Comment


    • #3
      Re: Script to Map Printers based on IP address of client

      Thanks very much. I'll check it out.

      We have multiple sites per subnet so I don't think that way would work.

      Michael
      Michael Armstrong
      www.m80arm.co.uk
      MCITP: EA, MCTS, MCSE 2003, MCSA 2003: Messaging, CCA, VCP 3.5, 4, 5, VCAP5-DCD, VCAP5-DCA, ITIL, MCP, PGP Certified Technician

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

      Comment


      • #4
        Re: Script to Map Printers based on IP address of client

        Code:
        Dim strSubnet, strComputer, WshNetwork
        Dim strPrinter1, strPrinter2, strPrinter3, strPrinter4
        Dim objWMIService
        
        Set WshNetwork = WScript.CreateObject("WScript.Network")
        
        strPrinter1 = "\\Server01\Printer01"
        strPrinter2 = "\\Server01\Printer02"
        strPrinter3 = "\\Server01\Printer03"
        strPrinter4 = "\\Server01\Printer04"
        
        On Error Resume Next
        WshNetwork.RemovePrinterConnection _
           strPrinter1, true, true
        WshNetwork.RemovePrinterConnection _
           strPrinter2, true, true
        WshNetwork.RemovePrinterConnection _
           strPrinter3, true, true
        WshNetwork.RemovePrinterConnection _
           strPrinter4, true, true
        On Error GoTo 0
        
        strComputer = WshNetwork.ComputerName
        
        Set objWMIService = GetObject("winmgmts:" _
           & "{impersonationLevel=impersonate}!\\" & strComputer _
           & "\root\cimv2")
        
        strSubnet =  GetSubnet(strComputer)
        
        'Assign printers to laptop users
        Select Case strSubnet
            Case "10.10.1.0/24"
                WSHNetwork.AddWindowsPrinterConnection strPrinter1
        
            Case "10.10.2.0/24"
                WSHNetwork.AddWindowsPrinterConnection strPrinter2
        
            Case "10.10.3.0/24"
                WSHNetwork.AddWindowsPrinterConnection strPrinter3
        
            Case "10.10.4.0/24"
                WSHNetwork.AddWindowsPrinterConnection strPrinter4
        
        End Select
        
        wscript.Quit
        
        
        Function GetSubnet(sHost)
           ' http://en.wikipedia.org/wiki/Subnetwork
           Dim arrNetworkAddress, prefix
           arrNetworkAddress = GetIpConf(sHost)
           Dim ActiveIPAddress, ActiveSubnetMask
           ActiveIPAddress = arrNetworkAddress(0)
           ActiveSubnetMask = arrNetworkAddress(1)
           Dim addressbytes(4), addressbyte
           Dim subnetmaskbytes(4), subnetmaskbyte
           Dim subnet, i, period, prevperiod
           i=0 : period = 1
           while period<>len( ActiveIPAddress ) + 2
             prevperiod=period
             period = instr( period+1, ActiveIPAddress, "." ) + 1
             if period = 1 then period = len( ActiveIPAddress ) + 2
             addressbyte = mid( ActiveIPAddress, prevperiod, _
               period-prevperiod-1 )
             addressbytes(i)=addressbyte
             i=i+1
           wend
           i=0 : period = 1
           while period<>len( ActiveSubnetMask ) + 2
             prevperiod=period
             period = instr( period+1, ActiveSubnetMask, "." ) + 1
             if period = 1 then period = len( ActiveSubnetMask ) + 2
             subnetmaskbyte = mid( ActiveSubnetMask, prevperiod, _
               period-prevperiod-1 )
             subnetmaskbytes(i)=subnetmaskbyte
             i=i+1
           wend
           subnet=""
               for i=0 to 3
                 subnet = subnet & (addressbytes(i) _
                   AND subnetmaskbytes(i)) & "."
               next
           GetSubnet = left( subnet, len(subnet)-1 )
           prefix = PrefixLength(ActiveSubnetMask)
           GetSubnet = GetSubnet & "/" & prefix
        End Function
        
        Function GetIpConf(sHost)
           ' objWMIService has global scope
           If not IsObject(objWMIService) Then
             Dim objWMIService
             Set objWMIService = GetObject("winmgmts:{impersonationLevel=" _
             & "impersonate}!\\" & sHost & "\root\cimv2")
           End If
           DIM colPings, objPing, sProtocolAddress, colNicConfigs
           DIM objNicConfig, i, sIPAddress, IsMatch, sSubnetmask
           DIM objNic, strIPAddress, arrsubnetmask
           'DETERMINE THE *ACTIVE* IP ADDRESS
           Set colPings = objWMIService.ExecQuery _
            ("Select * From Win32_PingStatus where Address = '" & sHost & "'")
           For Each objPing in colPings
             If objPing.StatusCode = 0 Then
               sProtocolAddress = objPing.ProtocolAddress
               Exit For
             Else
               WScript.Quit
             End If
           Next
           'GET SUBNETMASK FROM THE *ACTIVE* NIC
           Set colNicConfigs = objWMIService.ExecQuery _
             ("SELECT * FROM Win32_NetworkAdapterConfiguration " _
             & "WHERE IPEnabled = True")
           For Each objNicConfig In colNicConfigs
             IsMatch = False : i = 0 
             Set objNic = objWMIService.Get _
              ("Win32_NetworkAdapter.DeviceID=" & objNicConfig.Index)
             For Each strIPAddress In objNicConfig.IPAddress
               sIPAddress = strIPAddress
               If sIPAddress = sProtocolAddress Then 
                 IsMatch = True
                 sSubnetmask = objNicConfig.IPSubnet(i)
                 Exit For
               End If
               i = i + 1
             Next
             If IsMatch = True Then Exit For
           Next
           GetIpConf = array(sIPAddress,sSubnetmask)
        End Function
        
        Function PrefixLength(subnetmask)
           DIM arrsubnetmask, i, bits
           arrsubnetmask = split(subnetmask, ".")
           PrefixLength = 0
           For i = LBound(arrsubnetmask) To 3
             Select Case arrsubnetmask(i)
               case 0   bits = 0
               case 128 bits = 1
               case 192 bits = 2
               case 224 bits = 3
               case 240 bits = 4
               case 248 bits = 5
               case 252 bits = 6
               case 254 bits = 7
               case 255 bits = 8
             End Select
             PrefixLength = PrefixLength + bits
           Next
        End Function
        \Rems
        Last edited by Rems; 23rd December 2009, 09:24.

        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


        • #5
          Re: Script to Map Printers based on IP address of client

          Rems,

          Thanks for that. I'll take a look

          Michael
          Michael Armstrong
          www.m80arm.co.uk
          MCITP: EA, MCTS, MCSE 2003, MCSA 2003: Messaging, CCA, VCP 3.5, 4, 5, VCAP5-DCD, VCAP5-DCA, ITIL, MCP, PGP Certified Technician

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

          Comment

          Working...
          X