Link to home
Start Free TrialLog in
Avatar of stablerm
stablerm

asked on

Map printers bases on OU membership

This has been driving me nuts! Have just migrated from Netware to Win2Kserver/XP network. All I want is to assign a printer to a COMPUTER, not a user, based on the OU the computer is in. I manage a school network and do not want to give users access to add their own printers. I want them to be able to move from classroom to classroom and see the nearst printer - and only that one. I have created OU's for machines in geographical "zones" such as a teaching lab etc. I thought it would be simple to assign a startup script to map a printer with a persistent connectio so that any user logging on in that location would see only that printer etc. My users roam all over the building and are young and cannot cope with setting their own printers or be confronted with mappings to all devices in the building. I need to be able to simply script a printer to an OU and remove all others form their profile (roaming).

Would appreciate a solution as have wasted hours on this already

Thx in advance
Avatar of drev001
drev001

I've done this based on the Computer name using Kix (http://www.kixscripts.com). This is the script I use:

$usr = GetObject("WinNT://"  + @domain + "/"  + @wksta + "$$")
if @error <> 0
? @serror
else
For Each $grp In $usr.Groups
$GrpName = $grp.Name
if left($GrpName, 4) = "prt_"
$printer = substr($GrpName, 5)
$printer = join(split($printer,"~" ),"\" )
AddPrinterConnection("\\"  + $printer)
endif
Next
endif

In AD you create a Computer group for every network printer and name it in the following format: printserver~printersharename

Next, decide which computers need access to which printers. Then, add the computers into the relevant groups.

I've never done this based on OU membership but you may be able to change it to suit.

To remove all existing printers, you could use CON2PRT in your login script (see http://www.jsiinc.com/SUBA/tip0400/rh0458.htm)

Slight mistake there, the Computer group format should be prt_printserver~printersharename


Avatar of stablerm

ASKER

Thanks will try it. Would prefer an OU solution though if anyone has one. Have been working with a vbscript that queries AD for parent container and then is supposed to assign printers based on that OU memebrship but can't get it to work. Will post it tomorrow as am logged on at home and don't have access here. Hopefilluy someone can tell me waht is wrong with it. It amazes me that this would be so difficut in this day and age.

OK script I'm using is below. Part that maps drives based on OU is fine. Computer OU check works but no printers added. Any ideas?

____________________________________________________________________

On Error Resume Next

Set objNetwork = CreateObject("WScript.Network")
Set objAdsSystemInfo = CreateObject("adsysteminfo")
'gather computer OU info
Set objComputerName = Getobject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objCompOU = GetObject(objComputerName.Parent)
strCompOU = replace(objCompOU.Name,"OU=","")
'gather user OU info
Set objUserName = Getobject("LDAP://" & objAdsSystemInfo.UserName)
Set objUserOU = GetObject(objUserName.Parent)
strUserOU = replace(objUserOU.Name,"OU=","")

MsgBox "COMP OU membership is " & strCompOU
MsgBox "USER OU membership is " & strUserOU

'Map common drives
objNetwork.MapNetworkDrive "p:", "\\prepfs\publicstudents",True
objNetwork.MapNetworkDrive "f:", "\\prepfs\apps",True



'Check user OU and map OU specific drives

For Each objUserOU In objUserName.Parent
 Select Case strUserOU
  Case "staff"
   objNetwork.MapNetworkDrive "q:", "\\prepfs\publicstaff",True
 

 End Select
Next


'Check Comp OU membership and map printers accordingly


For Each objCompOU In objComputerName.Parent
 Select Case strCompOU
  Case "Computer Lab 1 walkers"
   
  objNetwork.AddWindowsPrinterConnection "\\Prepfs\W16OptraM412"

 End Select
Next



set objCompOU = Nothing
set objUserOU = Nothing
set objUserName = Nothing
set objComputerName = Nothing
set objAdsSystemInfo = Nothing
set objNetwork = Nothing

WScript.quit

__________________________________________________________________
ASKER CERTIFIED SOLUTION
Avatar of following
following
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another thing you might try if you want to use your current script:

Don't use the strCompOU, just try it like this, adding "OU=" to the Case:

For Each objCompOU In objComputerName.Parent
 Select Case objCompOU.Name
  Case "OU=Computer Lab 1 walkers"

Hope this helps,
-jdm
Here is one more idea that we use heavily -- also in a school district environment; this cannot be scripted (that I know of) and thus must be included on the image, which is fine if you do imaging:

Add the printer for a geographical area to all the computers in that area.  This can be done for network printers on a per-computer basis rather than a per-user basis, which is what happens with roaming profiles and/or logon scripts.  If the profile or script is unavailable (i.e. network problems), then the printer is not assigned.  This way, even if the connection to the server that holds the logon scripts is down, the user is still connected to the appropriate printer (if the print server is still up).

 - Use "Add a printer" in Start -> Settings -> Printers and Faxes
 - Choose "Local Printer..."
 - Choose "Create a new port:" (select Local Port)
 - Type in the full UNC name of the print queue (e.g. \\Prepfs\W16OptraM412)
 - Choose the appropriate driver

Now you will have a "local" printer that actually points to the network print queue, yet is accessible to each user that logs on, just as if it were a true local printer!

Drawbacks to this method:
 - Must be configured for each computer rather than centrally, like a logon script
 - No automatic downloading of drivers from the server: this means that if you want to update the drivers, you must do it on each and every computer
Okay,

Managed to get the script working fine. Have one last nagging problem. Have added a line to map H: to home directory based on user name. However My documents is insde that and I want users to have H mapped to My docs not one level up at their user name as it is with the My docs folder itself. I need both due to some legacy apps that can't see My docs - only drive letters. The thing is with small kids using it it must be the same or they will get confused. Have tried concatenating "\My Documents" to the end of the line and it does not work.

Line that works

objNetwork.MapNetworkDrive "h:", "\\prepfs\homedir$\" & strUserName , True

Maps to the username folder OK

Line that does not

objNetwork.MapNetworkDrive "h:", "\\prepfs\homedir$\" & strUserName & "\My Documents", True

Have used a variable and message box to evaluate this string and it comes out as expected with \\prepfs\homedir$\My Documents as being the result for the path.

Any ideas?

Are you running this script on pre-w2k clients?  If so, you will need to use DFS or share the actual folder you would like to map to.

Also, you said "it comes out as expected with \\prepfs\homedir$\My Documents as being the result for the path": Is it not getting a username perhaps?  Shouldn't it be \\prepfs\homedir$\<username>\My Documents when you evaluate the string?

Hope this helps,
-jdm
Sorry that was a typo should have been  \\prepfs\homedir$\<username>\My Documents. Problem is that script with not map to this folder - only to the level above i.e \\prepfs\homedir$\<username> any ideas?
What OS are the clients that run this script?

-jdm
XP
Make a script with this


'Begin
On Error Resume Next

Set objWshNetwork = CreateObject( "WScript.Network" )
Set objWshShell   = CreateObject( "WScript.Shell" )
Set objNetwork = CreateObject("WScript.Network")
Set objAdsSystemInfo = CreateObject("adsysteminfo")
'gather computer OU info
Set objComputerName = Getobject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objCompOU = GetObject(objComputerName.Parent)
strCompOU = replace(objCompOU.Name,"OU=","")
'gather user OU info
Set objUserName = Getobject("LDAP://" & objAdsSystemInfo.UserName)
Set objUserOU = GetObject(objUserName.Parent)
strUserOU = replace(objUserOU.Name,"OU=","")


'''''''''''''''''''''''''''''''''''''''''''''''''''
'  Maps Printers and Drives
''''''''''''''''''''''''''''''''''''''''''''''''''
'repeat for each OU that you want to do...can use diff printers and drives
If objUserOU( "Users" ) Then
    Call MapDrive ("T:", "ServerName", "Share")
    Call AddPrinter ("Domain", "ServerName", "Printername")
End If

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Sub:      AddPrinter
'
' Purpose:  Connect to shared network printer
'
' Input:
'           strPrtServerDomain  Domain in which print server is a member
'           strPrtServer        Name of print server
'           strPrtShare         Share name of printer
'
' Output:
'
' Usage:
'           Call AddPrinter ("Domain", "Server", "Printer")
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub AddPrinter(strPrtServerDomain, strPrtServer, strPrtShare)

  On Error Resume Next

  Dim strPrtPath    'Full path to printer share
  Dim objPrinter    'Object reference to printer
  Dim strMsg        'Message output to user
  Dim blnError      'True / False error condition

  blnError = False

  'Build path to printer share
  strPrtPath = "\\" & strPrtServer & "\" & strPrtShare

  'Test to see if shared printer exists.
  'Proceed if yes, set error condition msg if no.
  Set objPrinter = GetObject _
    ("WinNT://" & strPrtServerDomain & "/" & strPrtServer & "/" & strPrtShare)
  If IsObject( objPrinter ) AND _
  (objPrinter.Name <> "" AND objPrinter.Class = "PrintQueue") Then

     'Different mapping techniques depending on OS version
     If objWshShell.ExpandEnvironmentStrings( "%OS%" ) = "Windows_NT" Then
       Err.Clear
       'Map printer
       objWshNetwork.AddWindowsPrinterConnection strPrtPath
     Else
       'Mapping printers for Win9x & ME is a pain and unreliable.
     End If

  Else
    blnError = True
  End IF

 Set objPrinter = Nothing

End Sub



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Sub:      MapDrive
'
' Purpose:  Map a drive to a shared folder
'
' Input:
'           strDrive    Drive letter to which share is mapped
'           strServer   Name of server that hosts the share
'           strShare    Share name
'
' Output:
'
' Usage:
'           Call MapDrive ("X:", "Server", "Share")
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MapDrive( strDrive, strServer, strShare )

  On Error Resume Next

  Dim strPath       'Full path to printer share
  Dim blnError      'True / False error condition

  blnError = False

  'Disconnect Drive if drive letter is already mapped.
  'This assures everyone has the same drive mappings

  If objFileSys.DriveExists(strDrive) = True Then
    objWshNetwork.RemoveNetworkDrive strDrive, , True
  End If

  'Build path to share
  strPath = "\\" & strServer & "\" & strShare

  'Test to see if share exists. Proceed if yes, set error condition if no.
  If objFileSys.DriveExists(strPath) = True Then
    Err.Clear
    objWshNetwork.MapNetworkDrive strDrive, strPath
  Else
    blnError = True
  End If
End Sub

set objCompOU = Nothing
set objUserOU = Nothing
set objUserName = Nothing
set objComputerName = Nothing
set objAdsSystemInfo = Nothing
set objNetwork = Nothing
Set objWshNetwork  = Nothing
Set objWshShell    = Nothing

  'Exit script
  Wscript.Quit()


Hope that works
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial