Map Printer in Windows 7 using scripts

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

Who is Participating?
x3manConnect With a Mentor Commented:
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.
You could forget scripts and use Group Policy Preferences instead?
buddlesAuthor Commented:
Any idea how I can do this in Server 2003?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Sorry, you are using 2003. You need a server 2008 environment for GP preferences. Have you considered using the Print Management Console? See
buddlesAuthor Commented:
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
buddlesAuthor Commented:
Drive mappings connect fine in Windows 7 using my original vbs script

Priteshjchauhan - thanks for the link, however is this applicable for Windows 7?
spinzr0Connect With a Mentor Commented:
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

buddlesAuthor Commented:
Thanks Spinzr0.  Excuse my ignorance but do you mean this should be added to the logon script?
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()
did that work for you?
buddlesAuthor Commented:
It didn't no.  Should this go before the mapprn() in the main section or the Function mapprn() further down?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.