Link to home
Start Free TrialLog in
Avatar of buddles
buddles

asked on

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

Avatar of x3man
x3man

You could forget scripts and use Group Policy Preferences instead?
Avatar of buddles

ASKER

Any idea how I can do this in Server 2003?
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
Avatar of buddles

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of x3man
x3man

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of buddles

ASKER

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

Priteshjchauhan - thanks for the link, however is this applicable for Windows 7?
SOLUTION
Avatar of spinzr0
spinzr0
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of buddles

ASKER

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?
Avatar of buddles

ASKER

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