Map Printer in Windows 7 using scripts

Posted on 2010-09-24
Last Modified: 2013-01-16
Windows Server 2003 R2 environment.  Logon script used to map a printer to a PC by location.  Security group in AD contain the PCs which are used in the scripts to map the nearest printer.

Works fine in XP but Windows 7 PCs have nothing mapped.

When the script is run locally, the print spooler stops and is then unable to be restarted.  When done with an Admin it sometimes maps the printers, but not always.  

Any assistance would help...
'Decalre and Define Global variables
Dim coldrives
Dim strDrive
Dim FileSysObj
Dim WSHShell
Dim WSHNetwork
Dim objWMIService
Dim colInstalledPrinters
Dim error
Dim MsgBox_Title
Dim MsgBox_Text
Dim sGroup
Dim strComputer
Dim strPrinterPath
Dim comp
Dim pc

CRLF = Chr(13) & Chr(10)
strDrive = Chr(64)

'Set Objects
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WSHNetwork = WScript.CreateObject("WScript.Network")

'Main Body of Code
	'supress error messages
	on error resume next
'Declare functions
Function ok()
	'Enumerate network drives
	Set colDrives = WSHNetwork.EnumNetworkDrives

	If IsMember("colv_staff") Then
		If (FileSysObj.DriveExists("Z:") = False) Then
		End If
	End If
	pc = WSHNetwork.ComputerName
End Function

'----------------REMOVE NETWORK DRIVES------------------
Function removedrv()
Set colDrives = WSHNetwork.EnumNetworkDrives

	If colDrives.Count = 0 Then
		For i = 0 To colDrives.Count - 1 Step 2
			strDrive = colDrives(i)
			WSHNetwork.removeNetworkDrive strDrive
	End If
End Function

'--------------- REMOVE NETWORK PRINTERS ---------------
Function removeprn()
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colInstalledPrinters =  objWMIService.ExecQuery ("Select * from Win32_Printer")
	For Each objPrinter In colInstalledPrinters
		If objPrinter.Location <> Empty Then
		End If
End Function

'--------------- MAP NETWORK DRIVES ---------------
Function mapdrives()
	on error resume next
	WSHNetwork.mapnetworkdrive "Z:","\\colv-FS\User_files"
End Function

'--------------- MAP Accounts DRIVES ---------------
Function mapaccounts()
	on error resume next
	WSHNetwork.mapnetworkdrive "Y:","\\colv-MAIN\accounts"
End Function

'--------------- CHECK NETWORK DRIVES EXIST ---------------
Function nodrives()
	strMsg = "Could not map drive(s):" & CRLF			
	If FileSysObj.DriveExists("Z:") = False Then strMsg = strMsg & CRLF & "Z:" & Chr(9) & "\\colv-FS\User_files" End If
	MsgBox 	"ERROR!!! Could not map one or more drives to your staff account" & CRLF & _
		"You will NOT be able to access or save your work!!!" & CRLF & _
		CRLF & _
		strMsg, VBSystemModal + vbExclamation + vbOkonly, MsgBox_Title
End Function

'--------------- MAP NETWORK PRINTER (OU DEPENDENT)---------------
Function mapprn()

			If IsComp("Printers") Then

				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT02 - HP LaserJet 2420n (B&W)"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT03 - HP LaserJet 2600n (Coloured)"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT06 - Ricoh Copier 2022 (B&W)"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT07 - HP Color LaserJet 2605dn"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT08 - HP Color LaserJet 2605dn"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT09 - EPSON AL-CX11"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT10 - Ricoh Colour Copier MP C2000"
				WshNetwork.AddWindowsPrinterConnection "\\colv-MAIN\colvPRT11 - SHARP MX-2600N PCL6"

			End If

			If IsComp("UpstairsPrinters") Then
				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT02 - HP LaserJet 2420n (B&W)"
			End If

			If IsComp("DownstairsPrinters") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT03 - HP LaserJet 2600n (Coloured)"
			End If

			If IsComp("AccountsPrinters") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT11 - SHARP MX-2600N PCL6"
			End If

			If IsComp("ITPrinters") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT11 - SHARP MX-2600N PCL6"
			End If

			If IsComp("Room1Printer") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT08 - HP Color LaserJet 2605dn"
			End If

			If IsComp("Room2Printer") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT09 - EPSON AL-CX11"

			End If

			If IsComp("Room3Printer") Then

				WshNetwork.SetDefaultPrinter "\\colv-MAIN\colvPRT07 - HP Color LaserJet 2605dn"
			End If

If strPrinterPath <> Empty Then
		WshNetwork.AddWindowsPrinterConnection strPrinterPath
'		WshNetwork.SetDefaultPrinter strPrinterPath
	End If

End Function

'--------------- CHECK COMPUTER EXISTS IN A PRINTER GROUP ---------------
Function IsComp(sGroup)
	Dim aAdsPath, GroupObj, UserObj
	sAdsPath = WSHNetwork.UserDomain
	Set GroupObj = GetObject("WinNT://" & sAdsPath & "/" & sGroup)
	For Each UserObj in GroupObj.Members
		If LCase(UserObj.Name) = LCase(pc) & "$" Then
			IsComp = True		
		End If
	Set GroupObj = Nothing
End Function

'--------------- CHECK GROUP MEMBERSHIP ---------------
Function IsMember(sGroup)
    Dim sAdsPath, oUser, oGroup

    If IsEmpty(oGroupDict) Then
        Set oGroupDict         = CreateObject("Scripting.Dictionary")
        oGroupDict.CompareMode = vbTextCompare

        sAdsPath  = WSHNetwork.UserDomain & "/" & WSHNetwork.UserName
        Set oUser = GetObject("WinNT://" & sAdsPath & ",user")

        For Each oGroup In oUser.Groups
            oGroupDict.Add oGroup.Name, "-"

        Set oUser = Nothing
    End If

    IsMember = CBool(oGroupDict.Exists(sGroup))

End Function

Open in new window

Question by:buddles
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1

Expert Comment

ID: 33752191
You could forget scripts and use Group Policy Preferences instead?

Author Comment

ID: 33752204
Any idea how I can do this in Server 2003?

Expert Comment

ID: 33752208
Sorry, you are using 2003. You need a server 2008 environment for GP preferences. Have you considered using the Print Management Console? See
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

ID: 33752353
Thanks for the suggestion but is there a reason why the existing method it does not work in Windows 7 but does with XP?  

All machines are 32 bit

Accepted Solution

x3man earned 250 total points
ID: 33752540
There are fundamental changes in the security in Windows 7 compared to XP, e.g. UAC and so on. I won't pretend to be an expert with logon scripts but it could be something in the existing code syntax that Win 7 doesn't like, see : Are your drive mappings working for Win 7?

Either way I would choose to use the Printer Management Console to manage printers. I have used it before and found it was a simpler way of managing printers than writing scripts.

Author Comment

ID: 33753468
Drive mappings connect fine in Windows 7 using my original vbs script

Priteshjchauhan - thanks for the link, however is this applicable for Windows 7?

Assisted Solution

spinzr0 earned 250 total points
ID: 33753688
There is a reg key that needs to be modified to allow the printers to be installed.  Call this function before installing printers and you should be able to install the printers.
Sub SetPrinterPermissionsSettings()
    On Error Resume Next

    Const ADS_RIGHT_GENERIC_ALL       = &h10000000
    Const ADS_PATH_REGISTRY           = 3
    Const ADS_SD_FORMAT_IID           = 1

    Set oWShell = CreateObject("Wscript.Shell")
    oWShell.RegWrite "HKCU\Printers\LegacyPointAndPrint\DisableLegacyPointAndPrintAdminSecurityWarning", 1, "REG_DWORD"

    sRegKey = "HKCU\Printers\LegacyPointAndPrint"
    Set dacl = createobject("AccessControlList")
    Set sd = createobject("SecurityDescriptor")
    Set newAce1 = CreateObject("AccessControlEntry")
    Set newAce2 = CreateObject("AccessControlEntry")
    Set ace = CreateObject("AccessControlEntry")
    Set sdutil = createobject("ADsSecurityUtility")

    Set sd = sdUtil.GetSecurityDescriptor (sRegKey, ADS_PATH_REGISTRY, ADS_SD_FORMAT_IID)
    Set dacl = sd.DiscretionaryAcl

    For Each ace in dAcl
        dacl.RemoveAce ace
    newAce1.Trustee = "NT AUTHORITY\SYSTEM"
    newAce1.AccessMask = ADS_RIGHT_GENERIC_ALL

    dacl.AddAce newAce1

    newAce2.Trustee = "Administrators"
    newAce2.AccessMask = ADS_RIGHT_GENERIC_ALL

    dacl.AddAce newAce2
    sdutil.SetSecurityDescriptor sRegKey, ADS_PATH_REGISTRY, sd, ADS_SD_FORMAT_IID

    Set dacl = Nothing
    Set sd = Nothing
    Set newAce1 = Nothing
    Set newAce2 = Nothing
    Set ace = Nothing
    Set sdutil = Nothing
End Sub

Open in new window


Author Comment

ID: 33753939
Thanks Spinzr0.  Excuse my ignorance but do you mean this should be added to the logon script?

Expert Comment

ID: 33753971
yes.  Sorry, should've been more clear.  Add that function at the bottom and then just call it before you start installing printers with this line

Call SetPrinterPermissionsSettings()

Which would go before the line mapprn()

Expert Comment

ID: 33756847
did that work for you?

Author Comment

ID: 33767814
It didn't no.  Should this go before the mapprn() in the main section or the Function mapprn() further down?


Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn about cloud computing and its benefits for small business owners.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seveā€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question