Announcement

Collapse
No announcement yet.

printer mapping based on ip

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

  • printer mapping based on ip

    Hi Guys

    just registered on here but have been reading this site for sometime, a great resource so i thankyou for that

    i have been trying to adapt the script to work in our environment.

    We have 5 different locations with different ip/subnets

    whatever i do with this script i can only get it to map one printer, it does not map the second etc even if i put it on the same subnet

    any idea what changes i need to make for it to map more than one printer at a time

    i need for example 2 or so printers mapping for each subnet

    thanks for any help

    Code:
     
    Dim strSubnet, strComputer, WshNetwork
    Dim strPrinter1, strPrinter2, strPrinter3, strPrinter4
    Dim objWMIService
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    strPrinter1 = "\\DC\printer 1"
    strPrinter2 = "\\DC\printer2"
    strPrinter3 = "\\DC\printer 3"
    strPrinter4 = "\\DC\printer 4"
    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.0.1.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter1
        Case "10.0.1.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter2
        Case "10.0.1.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter3
        Case "10.0.1.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter4
    End Select
    wscript.Quit
     
    Function GetSubnet(sHost)
       ' 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
    Last edited by Rems; 14th July 2010, 18:06.

  • #2
    Re: printer mapping based on ip

    Code:
    Select Case strSubnet
    
        Case "10.0.1.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter1
            WSHNetwork.AddWindowsPrinterConnection strPrinter2
            WSHNetwork.AddWindowsPrinterConnection strPrinter3
    
        Case "10.10.2.0/24"
            WSHNetwork.AddWindowsPrinterConnection strPrinter1
            WSHNetwork.AddWindowsPrinterConnection strPrinter4
    
    End Select
    Last edited by Rems; 14th July 2010, 18:13.

    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: printer mapping based on ip

      Thankyou Rems that works now

      One other thing you maybe able to help with.

      I will give you an idea of what we are trying to achieve.

      We have various sites where receptionists move about they are all on different subnets so for example

      site 1 has an ip of 192.168.1
      site 2 has an ip of 192.168.1

      it is currently using the same ip as it is linked via wireless and straight into our network

      sometimes receptionists go to site 2 which have 2 different printers to site 1

      i need the script to recognise they are at site 2 and give them the correct printers but i am not sure how it would do that or if at all possible with it being on the same subnet.

      i suppose i could specify the direct ip of that pc on the reception?

      thanks for your help

      Comment


      • #4
        Re: printer mapping based on ip

        That would work if your machines have static IPs or have addresses reserved in DHCP. Given how much hassle both of those scenarios cause compared to the lack of any real advantages they offer, I have never seen any point to either.

        I would expect your PC naming convention to identify the site a machine is located at? If so, you could do it based on part of the hostname.
        Gareth Howells

        BSc (Hons), MBCS, MCP, MCDST, ICCE

        Any advice is given in good faith and without warranty.

        Please give reputation points if somebody has helped you.

        "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

        "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

        Comment


        • #5
          Re: printer mapping based on ip

          Hi guys

          well it seems i have come across a bigger problem. i didn't take into account that the users are using citrix to login. So once they are logged in this script is seeing the citrix server ip rather than the client ip

          is there anyway to overcome this?

          The machines do have static ip's and i do agree it is not ideal specifying each ip in the script

          they also are named computers to identify them through citrix, not sure how i would go about coding it though

          Comment


          • #6
            Re: printer mapping based on ip

            *bump*

            anyone know

            thanks

            Comment


            • #7
              Re: printer mapping based on ip

              it is not possible then

              Comment


              • #8
                Re: printer mapping based on ip

                Please don't bump threads. If anyone has something to contribute, they will do so. We all volunteer our time on this site - if you need an answer within a given time frame, hire a consultant.
                Gareth Howells

                BSc (Hons), MBCS, MCP, MCDST, ICCE

                Any advice is given in good faith and without warranty.

                Please give reputation points if somebody has helped you.

                "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

                "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

                Comment


                • #9
                  Re: printer mapping based on ip

                  fair enough. I thought it was common practice to do that bring attention to the thread if it falls behind into the pit of unreadable darkness. If people can't see the thread then they can't contribute. that was the only reason

                  apologies if i offended you

                  Comment

                  Working...
                  X