setup Script so users who logon get printer mapped.

I need help setting up a printers on a network.  I need all users who logon to the domain and terminal servers to get the same printers mapped.  How would I start and then what type of script would I use.  Should I add Netuse to logon script?  Thanks in advance.
LVL 1
Michael BaezSystem EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DraginMagikCommented:
i'd use vbscript.

create a GPO and assign it to the A/D group which contains your users.  open the GPO and navigate to UserConfig -> Policies -> Windows Settings -> Scripts.  go into 'login scripts'.  click 'show files' and note the location (perhaps put a shortcut to the folder on your desktop).  you will place your script in here.

i've attached the template i use for my login script.  it's a bit overkill for what you percisely asked, but if you need additional functions it should handle most anything you need.  you only need to change the part between the "Change Script Below" and "Change Script Above"  (line 107 i beleive).  

the below section would map the M: Drive and two printers for anyone belonging to the Executive group.

'###############################    CHANGE    ###############################
'###############################    SCRIPT    ###############################
'###############################    BELOW     ###############################


' Membership mapping
if isMember("Executive Team") Then
  DriveMapper "M:","\\Server1\ExecFolder"
  AddPrinter "\\PrintServer2\Executive"
  AddPrinter "\\PrintServer2\Accounting"
end if


'######################### ^^CHANGE SCRIPT ABOVE^^ ##########################




'$$$$ Version 2006-10-11 $$$$
'Compiled/Written by Johan Greefkes, 2004-2006


Option Explicit
Dim strLogName, bolWriteLog, bolManagePrinters, bolEnableCrossDomain
strLogName = "IRGLogonScript.log"    ' Filename of log to write to in the temp folder.
bolWriteLog = True           ' Toggles log-writing. I like it on, you may like it off.
bolManagePrinters = True     ' Disable for Windows 9x clients.
bolEnableCrossDomain = False  ' Toggles Cross Domain Authentication. Reads registry to obtain NetBIOS domain for the machine.

'Script Begins Here
Dim objFSO, objNetwork, objDrives, objDict, objComputer, objShell, objFileSystem, objLogFile, objRootDSE, objTrans, strPDrive 
Dim strComputerName, strDomain, strUser, strAdsPath, strNetBIOSDomain, strDNSDomain, strTemp, strLogPath, i
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("Wscript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
Set objShell = WScript.CreateObject("WScript.Shell")
strComputerName = objNetwork.ComputerName
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName
strAdsPath = strDomain & "/" & strUser

If bolEnableCrossDomain then
	On error resume next
	strNetBIOSDomain = objShell.RegRead _
		("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName")
	if Not err.number = 0 then
		MsgBox "Problem reading the computer's domain name from the Registry." & chr(10) & _
			"Please set ""bolEnableCrossDomain"" to False", vbCritical, "Logon Script Error"
		wscript.quit
	end If
	on error GoTo 0
Else
	strNetBIOSDomain = strDomain
end If


Set objComputer = GetObject("WinNT://" & strNetBIOSDomain & "/" & strComputerName & ",computer")
strTemp = objshell.ExpandEnvironmentStrings("%TEMP%")
strLogPath = strTemp & "\" & strLogName  '"
set objFileSystem = WScript.CreateObject("Scripting.FileSystemObject")
if bolWriteLog then 'initialize log file
	on error resume next
	Set objLogFile = objFileSystem.OpenTextFile(strLogPath, 2, True) '"
	writelog "############### Start Login Script ##################"
	if not err.number = 0 then
		msgbox "There was a problem opening the log file for writing." & chr(10) & _
			"Please check whether """ & strLogPath & """ is a valid file and can be openend for writing." & _
			chr(10) & chr(10) & "If you're not sure what to do, please contact your support person.",vbCritical, "Logon Script Error"
		wscript.quit
	end if
	on error goto 0
end if

'###############################              ###############################
'############################### INSTRUCTIONS ###############################
'###############################              ###############################
'Check for user group membership with isMember:
'isMember will write an entry in the log file when a user is in the group
'if isMember("groupname") then
'	commands here
'end if

'Check for computer group membership with isMember: (usefull for location specific printer creation)
'isComputerMember will write an entry in the log file when a user is in the group
'if isComputerMember("groupname") then
'	commands here
'end if

'Map drives with DriveMapper:
'DriveMapper "Drive:", "\\Server\share"

'Add Printers with AddPrinter:
'AddPrinter "\\Server\Printername"

'Set a default printer with SetPrinterDefault (printer has to exist, or this will not work)
'SetPrinterDefault "\\Server\Printername"

'Remove Printers with RemovePrinter:
'RemovePrinter "\\Server\Printername"

'Remove All Printers from a Server with RemovePrintersFromServer. Handy when you retire a windows print server.
'RemovePrintersFromServer "Servername"

'Execute any file with Run:
'Run "c:\windows\notepad.exe" 
'Run "\\server\share\script.vbs"

'Copy files with FileCopy(source, target):
'FileCopy "\\server\share\*.txt", "c:\temp\"
'NOTE: copies are not recursive, and wildcards can only exists in the file level.
'NOTE: Users must have permission to write files in the target location

'Write an entry to the log file with WriteLog():
'writelog("some text here")

'Set user environment variables with PutEnv:
'PutEnv "Variablename", "VariableValue"

'A log file will be placed in the user's temp directory and will 
'be named to the ScriptLogName variable set above.

'###############################    CHANGE    ###############################
'###############################    SCRIPT    ###############################
'###############################    BELOW     ###############################


' Membership mapping
if isMember("Executive Team") Then
  DriveMapper "M:","\\Server1\ExecFolder"
  AddPrinter "\\PrintServer2\Executive"
  AddPrinter "\\PrintServer2\Accounting"
end if


'######################### ^^CHANGE SCRIPT ABOVE^^ ##########################

'######################### Subs 'n Functions Below #########################
'############################## DO NOT CHANGE ##############################

writelog "################ End Login Script ###################"
Set objNetwork = Nothing
Set objDrives = Nothing
Set objComputer = Nothing
Set objShell = Nothing
Set objFileSystem = Nothing
Set objLogFile = Nothing
Wscript.Quit

Function IsMember(sGroup)
	Dim oDict, oUser, oGroup

	If IsEmpty(oDict) Then
		Set oDict = CreateObject("Scripting.Dictionary")
		oDict.CompareMode = vbTextCompare
		Set oUser = GetObject("WinNT://" & strAdsPath & ",user")
		For Each oGroup In oUser.Groups
			oDict.Add oGroup.Name, "-"
		Next
		Set oUser = Nothing 
	End If
	IsMember = CBool(oDict.Exists(sGroup))
	if IsMember and bolWriteLog then objLogFile.WriteLine(Now() & ": Success: User is member of " & sGroup)
End Function


Function IsComputerMember(sGroup)
	Dim oGroup
	on error resume next
	Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group")
    IsComputerMember = CBool(oGroup.IsMember(objComputer.ADsPath & "$"))
	if IsComputerMember and bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Computer is member of " & sGroup)
    Set oGroup = Nothing          
	If not Err.Number = 0 Then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed : IsComputerMember could not locate group " & sGroup)
	end if                                                              
	on error goto 0
End Function


sub WriteLog(sEntry)
	if bolWriteLog then objLogFile.WriteLine(Now() & ": Log:     " & sEntry)
End Sub


Sub DriveMapper(sDrive, sShare)
	For i = 0 To objDrives.Count -1 Step 2
	if LCase(sDrive) = LCase(objDrives.Item(i)) Then
		if not LCase(sShare) = LCase(objDrives.Item(i+1)) Then
			objNetwork.RemoveNetworkDrive sDrive, True, True
		Else
			if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect  """ & sDrive & """ to share """ & _
				sShare & """ (previously connected)")
			Exit Sub
		End if
	End If
	Next
	on error Resume Next
	objNetwork.MapNetworkDrive sDrive, sShare, True
	If Err.Number = 0 Then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect  """ & sDrive & """ to share """ & sShare & """")
	else
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Connect """ & sDrive & """ to share """ & sShare & """")
	end if
	on error goto 0
End Sub


sub AddPrinter(sPrinterUNC)
	on error resume next
	if not bolManagePrinters then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Printer connection skipped because disabled (" & _
			strUser & " on " & strComputerName & ")")
	else
		objNetwork.AddWindowsPrinterConnection sPrinterUNC
		If Err.Number = 0 Then
			if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect to printer: " & sPrinterUNC)
		 else
			if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Connect to printer: " & sPrinterUNC)
		end if
	end if
	on error goto 0
end sub


sub RemovePrinter(sPrinterUNC)
	dim oPrinters
	Set oPrinters = objNetwork.EnumPrinterConnections
	For i = 0 to oPrinters.Count - 1 Step 2
		if uCase(oPrinters.Item(i+1)) = uCase(sPrinterUNC) then 
			on error resume next
			objNetwork.RemovePrinterConnection sPrinterUNC, true, true
			If Err.Number = 0 Then
				if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & sPrinterUNC)
			else
				if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Remove printer: " & sPrinterUNC)
			end if
			on error goto 0
			exit sub
		end if	
	Next
	if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & sPrinterUNC & " (Printer did not exist)" )
	set oPrinters = Nothing
end sub

sub RemovePrintersFromServer(sServerName)
	dim oPrinters, aPrinter
	if bolWriteLog then objLogFile.WriteLine(Now() & ": Removing all printers from: " & ucase(sServerName))
	Set oPrinters = objNetwork.EnumPrinterConnections
	For i = 0 to oPrinters.Count - 1 Step 2
		on error resume next
			aPrinter = split(uCase(oPrinters.Item(i+1)),"\",-1, 1)  '"
			if not UBound(aPrinter) = 0 then  'this is not a local printer, process code
				if aPrinter(2) = ucase(sServerName) then
					objNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
					If Err.Number = 0 Then
						if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & oPrinters.Item(i+1))
					else
						if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Remove printer: " & oPrinters.Item(i+1))
					end if
				end if
			end if
		aPrinter = ""
		on error goto 0
	next
	if bolWriteLog then objLogFile.WriteLine(Now() & ": Removed  all printers from: " & ucase(sServerName))
end sub


sub SetPrinterDefault(sPrinterUNC)
	on error resume next
	if not bolManagePrinters then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Default printer to " & sPrinterUNC & _
			" skipped because disabled (" & strUser & " on " & strComputerName & ")")
	else
		objNetwork.SetDefaultPrinter sPrinterUNC
		If Err.Number = 0 Then
			if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Set as Default: " & sPrinterUNC)
		 else
			if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Set as Default: " & sPrinterUNC)
		end if
	end if
	on error goto 0
end sub


Sub PutEnv(sName, sValue)
	Dim oEnv
	Set oEnv = objShell.Environment("USER")
	oEnv(sName) = sValue
	if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Set user variable """ & sName & """ to: """ & sValue & """")
	Set oEnv = Nothing
End Sub


Sub Run(ByVal sFile)
	on error resume next
	objShell.Run Chr(34) & sFile & Chr(34), 1, false
	If Err.Number = 0 Then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Execute: " & sFile)
	 else
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Execute: " & sFile)
	end if
	on error goto 0
End Sub	


Sub FileCopy(sSource, sTarget)
	on error resume next
	objFileSystem.CopyFile sSource, sTarget
	If Err.Number = 0 Then
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Copy: " & sSource & " to " & sTarget)
	 else
		if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Copy: " & sSource & " to " & sTarget)
	end if
	on error goto 0
End Sub

Open in new window

baraneCommented:
Net Use would be enough . The alternate way is

 rundll32 printui.dll,PrintUIEntry /in /n "\\servername\printer name"  and finally save as batch file

ReneGeCommented:
Added /q (quiet mode, do not display error messages)

rundll32 printui.dll,PrintUIEntry /in /q /n "\\servername\printername"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Michael BaezSystem EngineerAuthor Commented:
OK I just looked at the customers logon script.  It is a batch file.  Can there be more than one logon script?  I would have to use net use.  If I have to setup multiple printers, can I get a example with multiple printers.  also I need the same script to run on domain and terminal servers.
ReneGeCommented:
The logon script will run where ever your user will logon on the domain.

You can add all your command lines to the same logon script. You dont need to create several.

To add multiple printers, just add the required sommand lines one after the other one. for example:
rundll32 printui.dll,PrintUIEntry /in /q /n "\\servername\Hp Laserjet 1100"
rundll32 printui.dll,PrintUIEntry /in /q /n "\\servername\Hp Laserjet 1200"

Cheers,
Rene
ReneGeCommented:
Glad I could help.

Thanks for the points.

Cheers,
Rene
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.