Announcement

Collapse
No announcement yet.

Assigning printer using IP

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

  • Assigning printer using IP

    Looking for some VBScript samples to use to map printers based on a local computer's IP subnet. This will run on a mixed bunch of XP/2000 machines in a Windows 2000/2003 active directory. Example: if I have three computers in different sites: 192.168.1.x, 192.168.2.x, 192.168.3.x, then I should be able to login and map to the printers at that are "closest or local" to it. I'm sure there is a way to do it, just haven't found anything yet. Appreciate the help.
    Last edited by Alvin; 15th May 2006, 15:32.

  • #2
    Re: Assigning printer using IP

    already made a start?
    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: Assigning printer using IP

      I'm very new to this. The question was asked to me late last week. I'll be googling tonight when I have some time, but I also need to pick up a book to get myself familiar with the basics. I already learned how to map a printer, just need to know what to use to pick up the IP address now and then I was thinking of some sort of if/and/or statement.

      Comment


      • #4
        Re: Assigning printer using IP

        Remember printers published in AD have a "Location" property you will be able to read. If you assign PCs to suitable OUs, you should be able to do something like this:

        For each printer in AD
        if pc.OU = printer.Location then
        assign printer
        else
        'do nothing
        end if
        Next printer

        Obviously this is not good code, just a rough structure for you to follow

        Hope it helps

        Tom
        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: Assigning printer using IP

          Windows 2000/2003 DNS server netmask ordering capability can bring you closer to the target.
          http://support.microsoft.com/?kbid=842197

          What I did in my infrastructure;

          1. I created identical DNS host records for all print servers

          print Host (A) 192.168.1.100
          print Host (A) 192.168.2.100
          print Host (A) 192.168.3.100
          .....

          2. I checked that Net mask ordering is enabled for DNS servers from every sites.
          3. I enabled Internet printing on all print servers and redirected http://print/printers to http://print

          Now, users from no matter witch location, only have to type http://print and they have all the printers available to connct to from there local site.
          Last edited by netxt; 16th May 2006, 17:37.
          Regards,
          Csaba Papp
          MCSA+messaging, MCSE, CCNA
          ...............................
          Remember to give credit where credit is due and leave reputation points where appropriate
          .................................

          Comment


          • #6
            Re: Assigning printer using IP

            Thanks for the responses. I'm trying to look through that link in the last post, then after re-reading what I posted, I realized how vague I was. Sorry about that. Here's what I am trying to do.

            Say Mr. I.T. Guy is mobile user is originally part of site A (192.168.1.x subnet), and his laptop is assigned printers A1 and A2, but not A3 (all in the 192.168.1.x subnet) through a login script.

            The company opens a new site B. Say now that Mr. I.T. Guy is jumping over to Site B (192.168.2.x subnet). He wants only printer B1, but not A1, A2 and A3.

            Then a few months later, site C (192.168.3.x) gets acquired. Mr. I.T. Guy throws a curve ball here. He wants all printers from all sites available, but wants printer C1 to be his default when he is plugged into the network at site C.

            The thing I don't understand is how can I make my script figure that out what site the laptop is at? Is there a command to return the IP address (the first three octets) of the laptop when it jumps from one site to the next? Or maybe there is a way to get the address of the local site's DNS server? This could be a one-liner to get the entire IP and a few more lines just to get rid of the last octet. I'm sure I'll kick myself in the butt as soon as I figure it out. If I get that line, I think I can handle the rest.

            Thanks!
            Last edited by Alvin; 17th May 2006, 04:11.

            Comment


            • #7
              Re: Assigning printer using IP

              In order to retrieve Ip address you should read Registry through vbscript.

              1. You should know How many interfaces you are using
              HKey_Local_Machine\SYSTEM\CurrentControlSet\Servic es\TCPIP\Parameters\Interfaces\
              2. Know which network card you are using.
              HKey_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards

              Comment


              • #8
                Re: Assigning printer using IP

                Now I know I've gotten myself in over my head when Thyagu threw that post at me - How do I account for a wireless card and an ethernet port in the same machine? One may have an IP, the other may have a private or 0 address assigned.

                I think I found something in MS TechNet and I'm going through it to pick off exactly what I need. I'll post my result possibly by Thursday night.

                Thyagu - thanks for pointing that detail out to me. It's something else for me to watch out for.
                Last edited by Alvin; 17th May 2006, 05:12.

                Comment


                • #9
                  Re: Assigning printer using IP

                  If a machine is getting ip from DHCP server then in the following path one particular inteface
                  HKey_Local_Machine\SYSTEM\CurrentControlSet\Servic es\TCPIP\Parameters\Interfaces\

                  will have a valid ip address with respect to a string value DHCPIPADDRESS and the string value for IPADDRESS will be "0.0.0.0"

                  else DHCPIPADDRESS="0.0.0.0" and IPADDRESS= <valid ip>

                  From this you can identify

                  Comment


                  • #10
                    Re: Assigning printer using IP

                    With a little bit of help from technet...LOL, I'm really bad with those IF statements, but the concept is there, I'll clean it up with ELSE later on.

                    Now I just need a bit of cleanup and what not. This script will keep adding printers for every valid adapter, so I'm trying to find a way to eliminate that from happening.

                    I decided to use the local gateways, site1, site2, site3 ... The echo lines are just for my testing purposes.

                    strComputer = "."
                    SITE1 = "192.168.1.1"
                    SITE2 = "192.168.2.1"
                    SITE3 = "192.168.3.1"

                    Set objWMIService = GetObject("winmgmts:" _
                    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

                    set colAdapters = objWMIService.ExecQuery _
                    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

                    n=1
                    WScript.Echo

                    For Each objAdapter in colAdapters

                    If Not IsNull(objAdapter.DefaultIPGateway) Then
                    For y = 0 to UBound(objAdapter.DefaultIPGateway)

                    If Not IsNull(objAdapter.IPAddress) Then
                    For i = 0 To UBound(objAdapter.IPAddress)
                    If SITE1 = objAdapter.DefaultIPGateway(y) Then
                    WScript.Echo " SITE1 IP address: " & objAdapter.IPAddress(i)
                    'Add printers here
                    End If

                    If SITE2 = objAdapter.DefaultIPGateway(y) Then
                    WScript.Echo " SITE2 IP address: " & objAdapter.IPAddress(i)
                    'Add printers here
                    End If

                    If SITE3 = objAdapter.DefaultIPGateway(y) Then
                    WScript.Echo " SITE3 IP address: " & objAdapter.IPAddress(i)
                    'Add printers here
                    End If

                    Next
                    End If
                    Next
                    End If

                    n=n+1

                    Next
                    Last edited by Alvin; 17th May 2006, 16:23.

                    Comment


                    • #11
                      Re: Assigning printer using IP

                      This thread is exactly what I am going through at the moment. We are migrating users to a new building, the lan is now going to have different subnets per floor (rather than different physical sites) and I want to implement a similar strategy. Alvin, can you possibly let us know a link to the Technet page you found this script?

                      I was thinking the easiest way around this would be just to write a small batch file per floor using the con2prt utility in the old Zero Admin kit and then getting users to run it via an email link.

                      Comment


                      • #12
                        Re: Assigning printer using IP

                        Hope this simple version gives you some ideas. You'll probably want to add a lot more to this.



                        BAT FILE:

                        Code:
                        :GetIP
                        :: IP for primary adapter
                        setlocal
                        for /f "delims=: tokens=1-2" %%c in ('ipconfig /all ^| find "IP Address"') do set GetIP=%%d
                        endlocal & set GetIP=%GetIP:~1%
                        
                        start /w logon.vbs %GetIP%
                        VbScript: logon.vbs

                        Code:
                        Option Explicit
                        
                        Dim MyIP, SubNet, IpChunk
                        
                        On Error Resume Next
                        
                        Set wshNetwork = CreateObject("WScript.Network")
                        
                        MyIP = Wscript.Arguments.item(0)
                        
                        IpChunk = Split(MyIP,".")
                        SubNet = CInt(IpChunk(2))
                        
                        'Assuming IP's  192.168.1.0, 192.168.2.0, 192.168.3.0....
                        If SubNet = 1 Then Floor1
                        If SubNet = 2 Then Floor2
                        If SubNet = 3 Then Floor3
                        
                        Public Sub Floor1
                          'add your code here
                           wshNetwork.MapNetworkDrive "M:", "\\Server\NET-APPS"
                           wshNetwork.AddWindowsPrinterConnection "\\Server\HPLJ4200-flr1"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr2"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr3"
                        End Sub
                        
                        Public Sub Floor2
                           wshNetwork.MapNetworkDrive "M:", "\\Server\NET-APPS"
                           wshNetwork.AddWindowsPrinterConnection "\\Server\HPLJ4200-flr2"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr1"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr3"
                        End Sub
                        
                        Public Sub Floor3
                           wshNetwork.MapNetworkDrive "M:", "\\Server\NET-APPS"
                           wshNetwork.AddWindowsPrinterConnection "\\Server\HPLJ4200-flr3"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr1"
                           wshNetwork.RemovePrinterConnection "\\Server\HPLJ4200-flr2"
                        End Sub
                        
                        Wscript.quit

                        Comment

                        Working...
                        X