+ Post New Thread
Results 1 to 13 of 13
Windows 7 Thread, Using scripts to assign printers to computers in Technical; Hey guys, At th secondary school i work at, we currently use group policy to deploy printers to classrooms, but ...
  1. #1

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Using scripts to assign printers to computers

    Hey guys,

    At th secondary school i work at, we currently use group policy to deploy printers to classrooms, but lately we are frequently seeing the printers not adding. In some rooms, the printers will add on one day, but not the next, it's very temperamental. My manager has asked me to see if we could assign printers using startup/logon scripts. The network is using 2 Server 2008 R2 DC's, with a R2 print server. All clients are Windows 7 - with a mixture of 32bit and 64.


    Fortunately, i also work at a primary school, which uses scripts to deploy printers. I have looked at the GPO for printer deployment, and the printer scripts are running from User Configuration\Windows Settings\Scripts (Logon/Logoff). When i "edit" the scripts, it says this...

    '
    ' Printers.vbs - Windows Logon Script.
    Set objNetwork = CreateObject("WScript.Network")
    objNetwork.AddWindowsPrinterConnection "\\printserver\Printer"
    objNetwork.SetDefaultPrinter "\\printserver\Printer"

    The difference is, the primary school network is still on XP, with a 2003 DC and print server. Will this method still work for a windows 7 network with 2008R2 DC's, with a seperate print server? Thanks in advance for any information.

    Regards

    Harry Greatorex

  2. #2
    AlexB's Avatar
    Join Date
    Jul 2006
    Location
    Warwickshire
    Posts
    333
    Thank Post
    33
    Thanked 28 Times in 25 Posts
    Rep Power
    20
    We used GPP for printers initially, but found it often took too long to mount the printers after login and moved back to a script the way you describe above. What you have above should work fine, but if you fancy a bit more flexibility feel free to use this as a basis

    Code:
    ' Script to mount printers based on computer's OU
    ' 20/09/2010 by ACB
    ' Reads printers and OUs from a printers.ini file specified in the printers.ini file
    ' printers.ini file is set by the strINIFile variable
    strINIFile = "\\<server>\<share>(\<path>)\printers.ini"
    ' Default print server is read from the ini file in the [settings] section from the "server" key
    ' The computer's OU is then read from the [printers] section as a comma separated string
    ' The first entry in the printer string is used as the default printer
    ' If the OU is not found no error is returned and no additional printers are mapped
    ' There must be no spaces between printer share names in the comma separated string
    
    ' Custom printers (printers from different print servers) are read from the [custom] section
    ' The result from the [custom] section provides a comma separated string containing "print server, share name"
    ' Custom printers are currently set to be the default.
    ' The must be no space after the comma after the server name in the comma separated string
    
    ' Finally any other network printers are removed (rather than removing all network printers and reconnecting, this saves time)
    ' The variable boolRemoveOtherPrinter can be set to false to remove this functionality
    boolRemoveOtherPrinter = true
    
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    strName = objSysInfo.ComputerName
    
    arrComputerName = Split(strName, ",")
    arrOU = Split(arrComputerName(1), "=")
    strComputerOU = arrOU(1)
    strPrintServer = ReadIni( strINIFile, "settings", "server")
    
    
    Set objDictionary = CreateObject("scripting.dictionary")
    
    ' ********************
    ' ********************
    ' Temp fix to remove all printers first
    'RemoveOtherPrinters
    ' ********************
    ' ********************
    
    ' *********************
    ' Deal with LFTs differently to other computers
    ' *********************
    if StrComp("sba-lft",Mid(strName,4,7),1)=0  then
    	DebugPopup("LFT")
    	HandleLFTs
    else
    	DebugPopup("Others")
    	HandleComputers
    end if
    
    
    Function HandleComputers()
    	' *********************
    	' Read and mount printers for computer's OU
    	strPrinters = ReadIni( strINIFile, "printers", strComputerOU)
    
    	listPrinters = Split(strPrinters, ",")
    
    	if UBound(listPrinters) >= 0 then
    		MapPrinter listPrinters(intX), true
    		For intX = 1 To UBound(listPrinters)
    			MapPrinter listPrinters(intX), false
    		Next
    	end if
    	' *********************
    
    
    	' *********************
    	' Look for custom printers
    	strCustomPrinter = ReadIni( strINIFile, "custom", strComputerOU)
    
    	listCustomPrinters = Split(strCustomPrinter, ",")
    
    
    	if UBound(listCustomPrinters) = 1 then
    		' string has correct format
    		MapPrinterCustom listCustomPrinters(1), listCustomPrinters(0), true
    	end if
    	' *********************
    
    
    	' *********************
    
    	' Look for printers for everyone
    	strEvPrinters = ReadIni( strINIFile, "everyone", "Printers")
    
    	listEvPrinters = Split(strEvPrinters, ",")
    
    
    	if UBound(listEvPrinters) >= 0 then
    		For intX = 0 To UBound(listEvPrinters)
    			MapPrinter listEvPrinters(intX), false
    		Next
    	end if
    	' *********************
    
    
    	' *********************
    	' Remove other network printers
    	if boolRemoveOtherPrinter then
    		RemoveOtherPrinters
    	end if
    	' *********************
    
    End Function
    
    
    Function HandleLFTs()
    
    	' Could have a popup here if no network printers reminding users how to get printers
    End Function
    
    
    ' *********
    ' Functions
    ' *********
    Function MapPrinter(strPrinter,boolDefault)
    	Set objNetwork = CreateObject("WScript.Network")
    	if StrComp("local",strPrinter,1) <> 0 then
    		MapPrinterCustom strPrinter, strPrintServer, boolDefault
    	end if
    
    End Function
    
    
    
    Function MapPrinterCustom(strPrinter,strServer,boolDefault)
    
    	Set objNetwork = CreateObject("WScript.Network")
    	
    	strFullPath = UCase("\\" & strServer & "\" & strPrinter)
    	DebugPopup("Mounting:" & strFullPath)
    	objNetwork.AddWindowsPrinterConnection strFullPath
    	if boolDefault then
    		objNetwork.SetDefaultPrinter strFullPath
    	end if
    	
    	DebugPopup("Mounted:" & strFullPath)
    	objDictionary.Add strFullPath, strPrinter
    
    End Function
    
    Function IsInList(list, value)
    
    	IsInList = false
    	For i = 0 To UBound(list)
    		if UCase(list(i)) = UCase(value) then
    			IsInList = true
    		end if
    	next
    
    End Function
    
    Function RemoveOtherPrinters()
    	Dim objPrinters, intDrive, intNetLetter
    
    	' This is the heart of the script 
    	' Here is where objPrinters enumerates the mapped drives
    	Set objNetwork = CreateObject("WScript.Network") 
    	Set objPrinters = objNetwork.EnumPrinterConnections
    	strNoDelete = ReadIni( strINIFile, "nodelete", "Printers")
    	listNoDelete = Split(strNoDelete, ",")
    
    	If objPrinters.Count <> 0 Then 
    		
    		For i = 0 to objPrinters.Count - 1 Step 2
    
    			If Left(ucase(objPrinters.Item(i+1)),2) = "\\" Then
    				'if not listNoDelete.Contains(objPrinters.Item(i+1)) then
    				if not IsInList(listNoDelete, objPrinters.Item(i+1)) then
    					if not objDictionary.Exists(UCase(objPrinters.Item(i+1))) Then
    						DebugPopup("Leftover: " & objPrinters.Item(i+1))
    						objNetwork.RemovePrinterConnection objPrinters.Item(i+1)
    					end if
    				end if
    			end if
    		Next
    		
    	end if
    
    End Function
    
    ' e.g. RemovePrinter "\\<server>\<printer>"
    Function RemovePrinter(strPrinter)
    	DebugPopup("Removing " & strPrinter)
    	Set objNetwork = WScript.CreateObject("WScript.Network") 
    	objNetwork.RemovePrinterConnection strPrinter, true, true
    End Function
    
    Function ReadIni( myFilePath, mySection, myKey )
        ' This function returns a value read from an INI file
        '
        ' Arguments:
        ' myFilePath  [string]  the (path and) file name of the INI file
        ' mySection   [string]  the section in the INI file to be searched
        ' myKey       [string]  the key whose value is to be returned
        '
        ' Returns:
        ' the [string] value for the specified key in the specified section
        '
        ' CAVEAT:     Will return a space if key exists but value is blank
        '
        ' Written by Keith Lacelle
        ' Modified by Denis St-Pierre and Rob van der Woude
    
        Const ForReading   = 1
        Const ForWriting   = 2
        Const ForAppending = 8
    
        Dim intEqualPos
        Dim objFSO, objIniFile
        Dim strFilePath, strKey, strLeftString, strLine, strSection
    
        Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    
        ReadIni     = ""
        strFilePath = Trim( myFilePath )
        strSection  = Trim( mySection )
        strKey      = Trim( myKey )
    
        If objFSO.FileExists( strFilePath ) Then
            Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
            Do While objIniFile.AtEndOfStream = False
                strLine = Trim( objIniFile.ReadLine )
    
                ' Check if section is found in the current line
                If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
                    strLine = Trim( objIniFile.ReadLine )
    
                    ' Parse lines until the next section is reached
                    Do While Left( strLine, 1 ) <> "["
                        ' Find position of equal sign in the line
                        intEqualPos = InStr( 1, strLine, "=", 1 )
                        If intEqualPos > 0 Then
                            strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
                            ' Check if item is found in the current line
                            If LCase( strLeftString ) = LCase( strKey ) Then
                                ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
                                ' In case the item exists but value is blank
                                If ReadIni = "" Then
                                    ReadIni = " "
                                End If
                                ' Abort loop when item is found
                                Exit Do
                            End If
                        End If
    
                        ' Abort if the end of the INI file is reached
                        If objIniFile.AtEndOfStream Then Exit Do
    
                        ' Continue with next line
                        strLine = Trim( objIniFile.ReadLine )
                    Loop
                Exit Do
                End If
            Loop
            objIniFile.Close
        Else
            WScript.Echo strFilePath & " doesn't exists. Exiting..."
            Wscript.Quit 1
        End If
    End Function
    
    Function DebugPopup(strMessage)
    
    	'Set WshShell = CreateObject("WScript.Shell")
    	'WshShell.Popup strMessage, , , 64 
    
    End Function
    printers.ini
    Code:
    [settings]
    server = <server>
    
    [printers]
    OU = defaultPrinter,nextPrinter,nextPrinter,etc...
    
    [custom]
    OU = <server>,<printer name>
    
    [everyone]
    ;Printers = Printer,nextPrinter,etc...
    
    [nodelete]
    Printers = \\<server>\<printer>,\<server>\<printer>

  3. Thanks to AlexB from:

    Harry_Greatorex (7th November 2013)

  4. #3

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks for your reply. I will look into this and ill see how we get along.

  5. #4

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Right, I tried the script i posted up on a test computer room, but the script didn't seem to run automatically with the GPO i created, only when i ran it manually. Any idea why this is?

    Harry

  6. #5
    AlexB's Avatar
    Join Date
    Jul 2006
    Location
    Warwickshire
    Posts
    333
    Thank Post
    33
    Thanked 28 Times in 25 Posts
    Rep Power
    20
    You might need to disable 'Point and Print Restrictions' in GPO

  7. #6

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks for your reply. Could you tell me where to find this policy in GPO? I've heard it has the same policy in both user config and computer config.

  8. #7
    Duke5A's Avatar
    Join Date
    Jul 2010
    Posts
    731
    Thank Post
    74
    Thanked 113 Times in 99 Posts
    Blog Entries
    8
    Rep Power
    27
    Quote Originally Posted by Harry_Greatorex View Post
    Thanks for your reply. Could you tell me where to find this policy in GPO? I've heard it has the same policy in both user config and computer config.
    It's in the Computer policy. Computer Configuration > Policies> Administrative Templates > Printers > Point and Print Restrictions

    You only need to worry about this if you're running Windows Vista/7/8.

    We still deploy our printers via script as well since GPP proved to be unreliable. Besides, using a VB script allows for much better granular control without making a mess of AD. I made a sample script in a blog post here: VB Script for network printer deployment - Blogs - EduGeek.net

  9. #8

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Right, after a bit of testing - my printer script is STILL not running. I really don't know what to do now. I have changed P+P restrictions to allow the drivers to be installed, and only from our print server, but when i log in it's not running. Any Ideas?

    Harry

  10. #9
    AlexB's Avatar
    Join Date
    Jul 2006
    Location
    Warwickshire
    Posts
    333
    Thank Post
    33
    Thanked 28 Times in 25 Posts
    Rep Power
    20
    Have a script that just does this:-

    Code:
    Set objNetwork = CreateObject("WScript.Network")
    objNetwork.AddWindowsPrinterConnection "\\<server>\<printer>"
    If that works then you are able to mount printers via a vbs script and it must just be an error in the more complicated script.

  11. #10

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks AlexB! That now works and i have printers installed via the script. Now my next hurdle...is it possible to have these scripts running at start up, as a start up script, rather than as log on? I have a fair few computer rooms that have printers, and i need them added by VBS, but all the users are in a separate OU so i cannot use them as logon scripts.

  12. #11
    AlexB's Avatar
    Join Date
    Jul 2006
    Location
    Warwickshire
    Posts
    333
    Thank Post
    33
    Thanked 28 Times in 25 Posts
    Rep Power
    20
    As far as I know you can't mount printers at startup. However, with loop back processing you could make this a User GPO run a logon script, but allocate to computer OUs.

    Alternatively the script I posted above allows you to just name the computer OUs that will get what printers and apply the script to all users.

  13. #12

    Join Date
    Sep 2010
    Posts
    23
    Thank Post
    2
    Thanked 1 Time in 1 Post
    Rep Power
    0
    We use to have problems using the printers via GPO, but once we figured out the best settings/setup for it we've had no issues using GPO for printers and stopped using VBS scripts because they were too inconsistent. It looks like you've already moved on to the VBS scripts but if you ever need help with the GPO method I could share how we have it setup here in our school district.

  14. #13

    Join Date
    Jul 2013
    Location
    Burgess Hill
    Posts
    21
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    dmanning - yes if you could that would be great. Personally im not liking the VBS way and would rather stick to GPO, so this should prove very useful, thanks!

SHARE:
+ Post New Thread

Similar Threads

  1. Tip: Use Group Policy Preferences to deploy printers to terminal server clients
    By ZeroHour in forum Thin Client and Virtual Machines
    Replies: 0
    Last Post: 26th October 2010, 12:05 PM
  2. Restricting access to a printer to specific PCs
    By BatchFile in forum Network and Classroom Management
    Replies: 17
    Last Post: 27th February 2009, 11:30 AM
  3. Replies: 3
    Last Post: 25th October 2007, 09:14 AM
  4. Assign GPO to computer group
    By Andi in forum Wireless Networks
    Replies: 26
    Last Post: 29th June 2007, 02:57 PM
  5. Script to delete printer
    By tosca925 in forum Scripts
    Replies: 2
    Last Post: 31st August 2005, 07:45 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •