Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Map Printer in Windows 7 using scripts

Posted on 2010-09-24
Medium Priority
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.


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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…

715 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