Solved

Map Printer in Windows 7 using scripts

Posted on 2010-09-24
12
1,139 Views
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 CRLF

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

	removeprn()

	removedrv()

	ok()

	mapprn()

'--------------------------------------------------

'Declare functions

'--------------------------------------------------

Function ok()

	'Enumerate network drives

	Set colDrives = WSHNetwork.EnumNetworkDrives



	If IsMember("colv_staff") Then

		mapdrives()

		If (FileSysObj.DriveExists("Z:") = False) Then

				nodrives()

		End If

	End If

	pc = WSHNetwork.ComputerName

End Function



'----------------REMOVE NETWORK DRIVES------------------

Function removedrv()

Set colDrives = WSHNetwork.EnumNetworkDrives



	If colDrives.Count = 0 Then

	Else

		For i = 0 To colDrives.Count - 1 Step 2

			strDrive = colDrives(i)

			WSHNetwork.removeNetworkDrive strDrive

		Next

	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

			objPrinter.Delete_

		End If

	Next

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

	Next

	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, "-"

        Next



        Set oUser = Nothing

    End If



    IsMember = CBool(oGroupDict.Exists(sGroup))



End Function

Open in new window

0
Comment
Question by:buddles
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 9

Expert Comment

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

Author Comment

by:buddles
ID: 33752204
Any idea how I can do this in Server 2003?
0
 
LVL 9

Expert Comment

by:x3man
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 http://www.microsoft.com/downloads/en/details.aspx?FamilyID=83066DDC-BC96-4418-A629-48C8ABD2C7A0
0
 

Author Comment

by:buddles
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
0
 
LVL 9

Accepted Solution

by:
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 :http://serverfault.com/questions/95379/why-wont-my-logon-scripts-map-drives-under-windows-7. 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.
0
 

Expert Comment

by:priteshjchauhan
ID: 33752738
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:buddles
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?
0
 
LVL 8

Assisted Solution

by:spinzr0
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_ACETYPE_ACCESS_ALLOWED  = 0

    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

    Next

    newAce1.Trustee = "NT AUTHORITY\SYSTEM"

    newAce1.AccessMask = ADS_RIGHT_GENERIC_ALL



    newAce1.AceType = ADS_ACETYPE_ACCESS_ALLOWED

    dacl.AddAce newAce1



    newAce2.Trustee = "Administrators"

    newAce2.AccessMask = ADS_RIGHT_GENERIC_ALL



    newAce2.AceType = ADS_ACETYPE_ACCESS_ALLOWED

    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

0
 

Author Comment

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

Expert Comment

by:spinzr0
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()
0
 
LVL 8

Expert Comment

by:spinzr0
ID: 33756847
did that work for you?
0
 

Author Comment

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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now