Assign Printer on Location

Discussion in 'Windows Server Systems' started by jonny813, Jun 24, 2007.

  1. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    How could one assign a user printers based on location. At the moment I can assign printers to everyone which is not always convenient. E.g.Printers in the English department can be seen from a user in the Art department.
    How can i assign printers that are relevant to where the user is located at a certain department.
    I only have one PC running Windows Server 2003 Enterprise, would I need another? thnx
     
  2. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Are the users logging in on the same computer each time or are they moving around? Command scripting would work for the first, but not for the second because Active Directory doesn't know where the computer is technically located.
     
  3. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    They would be logging into many machines as each department has say 5 clients for that department.
    e.g. A user logs on (to any of the 5) at the English department and only have a choice to use printers in the English department. While a user doing the same thing in the ICT department only sees ICT department printers.

    Would creating a script under Computer Configuration do the trick, (and bin the ones I have set that show them all printers). So the script is run to all who log in to those particular machines? But as to how to apply the script to an actual Computer I'm not sure.
    hope thats clear
     
    Last edited: Jun 24, 2007
  4. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    I didn't think of that idea. Put each of those computers into a different OU and you should be good to go.
     
  5. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    It didn't come off unfortunately. It ran the Computer Settings on user logon but nothing came off, not even an error.Surely though it should have worked.
    Applying a startup script to a Computer to connect certain printers, it makes sense lol. Did you have another idea?
     
  6. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    From what I understand, you want whoever signs into an English dept computer to have the english dept printers connected and simlilar setups for other dept.

    You can actually do a couple things:

    1) Put computers into seperate dept based OU's
    2) Put computers into seperate dept base groups.

    In either case, you can use either a computer or user loginscript to map the printers. Computer startup may be a bit iffy since the computer may not have connected/authenticated/set up the secure session to the domain by the time the startup script runs (yes, computers authenticate to the domain!).

    Depending on how your group policies are set to process user login scripts, you might have the same problems with user login scripts since it is technically possible to set policies to process logins before the computer is fully "up" and on the domain. Trust me, it took me a long time to track down why our login scripts weren't properly pulling domain info during login until I traced it down to this.

    My advice would be to set your user login scripts to check either condition above on the computer account (either computer OU membership or computer group memberships) and map your printers accordingly.

    Again, you need some scripting advice, lets start with what you have and go from there.
     
  7. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    This is my current script as is:

    Set WshNetwork = CreateObject("WScript.Network")

    WshNetwork.AddWindowsPrinterConnection "\\JONNYSERV1\HP LaserJet 4100 Series PS"
    WshNetwork.SetDefaultPrinter "\\JONNYSERV1\HP LaserJet 4100 Series PS"
     
  8. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    As I think about it, a computer script won't work. When you connect/map a printer, it is tied to the user profile.. since a user profile isn't loaded when a computer startup script runs, the printer has no profile to connect to.

    You could, in theory, try to map a printer and copy that profile to the default profile. This will allow any new profile that is created to already have that printer connected. For existing users, you could either map them manually, or delete their profile and force them to generate a new one from the default profile (I don't recommend deleting existing profiles..). The downside of using the profile method, is anytime you add/replace/move a computer to a department, you'll have to remember to set this up each file.

    A script solution is still more ideal to me for this reason.

    Since a computer startup script won't work, you'll have to do it during the user login script. You still have to choices depending on your needs:

    1) Put computers into seperate OU's and check the computer OU during the login script and map printers accordingly.
    2) Put computers into groups and check computer group membership during the login script and map printers accordingly.

    Checking the OU is quicker/faster to script, but some environments don't allow/don't want computer's in OU's based on location.. thus checking group membership would be used. If you're workplace doesn't mind seperate OU's for each location/dept for the computer accounts, which it sounds like your location, then use the OU.

    Code:
    'returns the LDAP Path (distinguished name) of the computer account
    set objSysInfo = CreateObject("ADSystemInfo")
    strComputerDN = objSysInfo.ComputerName
    
    From there, it will return the full LDAP path of the computer account as a string (ie:"CN=MyComputerName,OU=Math,DC=Contessa,DC=com")
    You would then search the LDAP string for your OU and using either a case statement or if/then's map your printers accordingly.

    Searching the LDAP string can be done a number of ways depending on the complexity of your LDAP path and if you use commas in OU names, etc. If it's fairly simple (ie: no nested OU's, etc..)
    Code:
    'find where the OU section starts in the LDAP Path
    intStart=InStr(0,strComputerDN,"OU=")
    
    'if the OU section exists, process the ou name
    if intStart <> 0 then 
        intEnd=InStr(intStart,strComputerDN,",")
    
        'extract just the ou= portion
        strOU = mid(strComputerDN,intStart,intEnd)
    
        Set objNetwork= CreateObject("WScript.Network")
    
        'depending on which ou, map printers (converted ou string to all lower case)
        select case lcase(strOU)
            case "ou=english"
                objNetwork.AddWindowsPrinterConnect "<printer path>"
                objNetwork.AddWindowsPrinterConnect "<printer path2>"
                objNewwork.SetDefaultPrinter "<defaultPritnerPath>"
            case "ou=math"
                <map printers for computers in math dept.>
        end select
    end if
    
    edit:
    disclaimer - the code above is for example purposes only. It is only a skeleton/framework and really needs some error checking and other controls put in before being considered for production use.
     
    Last edited: Jun 25, 2007
  9. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    OK, I decided to try this out on my test box (Server 2003 R2)

    OU's:
    -PUBLIC (2 two clients- pub1, pub2)
    -NEO (1 client- neo-1)

    using the following:

    Code:
    set objSysInfo = CreateObject("ADSystemInfo")
    strComputerDN = objSysInfo.ComputerName
    intStart=InStr(0,strComputerDN,"PUBLIC=")
    if intStart <> 0 then 
        intEnd=InStr(intStart,strComputerDN,",")
    strOU = mid(strComputerDN,intStart,intEnd)
       Set objNetwork= CreateObject("WScript.Network")
    select case lcase(strOU)
            case "ou=PUBLIC"
                objNetwork.AddWindowsPrinterConnect "\\JONNYSERV\HP 2000C 1"
                objNetwork.AddWindowsPrinterConnect "\\JONNYSERV\HP 2000C 2"
                objNewwork.SetDefaultPrinter "\\JONNYSERV\HP 2000C 1"
            case "ou=NEO"
                objNetwork.AddWindowsPrinterConnect "\\JONNYSERV\HP 2000C 3"
                objNewwork.SetDefaultPrinter "\\JONNYSERV\HP 2000C 3"
          end select
    end if
    and got a error on:
    Line: 3
    Char: 1
    Error: Invalid procedure call or argument: 'InStr'
    Code: 800A0005
    Source: Microsoft VBScript Runtime Error

    Whats wrong? I know I'm such a pain on scripts
     
  10. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Try OU=PUBLIC
     
  11. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    Still get the same error...:cry:
     
  12. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    as a quick test, after you pull the objSysInfo.Computername , do a:

    "wscript.echo strComputerDN"

    and comment out the rest of the script. Make sure it is returning a valid value
     
  13. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    Like this?

    Code:
    set objSysInfo = CreateObject("ADSystemInfo")
    strComputerDN = objSysInfo.ComputerName
    "wscript.echo strComputerDN"
    intStart=InStr(0,strComputerDN,"ou=PUBLIC")
    if intStart <> 0 then 
        intEnd=InStr(intStart,strComputerDN,",")
    strOU = mid(strComputerDN,intStart,intEnd)
       Set objNetwork= CreateObject("WScript.Network")
    select case lcase(strOU)
            case "ou=PUBLIC"
                objNetwork.AddWindowsPrinterConnect "\\JONNYSERV\HP 2000C 1"
                objNetwork.AddWindowsPrinterConnect "\\JONNYSERv\HP 2000C 2"
                objNewwork.SetDefaultPrinter "HP 2000C 1"
            case "ou=NEO"
                objNetwork.AddWindowsPrinterConnect "HP 2000C 3"
                objNewwork.SetDefaultPrinter "HP 2000C 3"
          end select
    end if
    Error now reads: expected statement
     
  14. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    no.. without quotes around the wscript.echo ... and either delete or comment out the rest of the script
     
  15. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    Windows Script Host returned the following:
    Code:
    CN=PUB1,OU=PUBLIC,DC=TESTSERV,DC=local
    Seems fine to me
     
    Last edited: Jul 8, 2007
  16. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    well, let's start with trying to remove the start position so, your code would start like:
    Code:
    set objSysInfo = CreateObject("ADSystemInfo")
    strComputerDN = lcase(objSysInfo.ComputerName)
    intStart=InStr(strComputerDN,"ou=")
    
     
  17. jonny813

    jonny813 OSNN Addict

    Messages:
    88
    OK, should I execute that or what?
     
  18. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    that should just be the beginning of the script.. i expect you to do some work and thinking as well.. ;)
     
  19. Nismo83

    Nismo83 OSNN AZN Addict

    Messages:
    383
    not sure if this work but after sharing the printer, there's this security settings where we can set a specifically user to print, manage documents and etc.
     
  20. ScottFern

    ScottFern IT guy

    Messages:
    48
    Location:
    Chicago
    I tried this command as a login script but I received an error:

    rundll32 printui.dll,PrintUIEntry /ga /in /n\\hubdc1\AcctLJ2200 /y

    Anything wrong with this command? Sorry to threadjack!