Friday, May 18, 2012

Create Network Printers at Logon With vbs and Batch File

Create Network Printers at Logon With vbs and Batch File


The following vbs script can be run from a batch file like a logon.bat file that you may already have in your NETLOGON folder. Or simply create a new one. Be sure to add the logon.bat to your users' account  profiles in AD.  You'll need to get the fmember exe from Microsoft, or you may already have it.


Set WshNetwork = CreateObject("WScript.Network")

Set objShell = CreateObject("Wscript.Shell")

WSHNetwork.MapNetworkDrive "N:", \\DC01\Netlogon

WshNetwork.RemovePrinterConnection \\PrintServer01\Printer1
WshNetwork.RemovePrinterConnection \\PrintServer01\Printer2

strCommand = "%comspec% /c N:\IfMember.exe FirstFloor"
intReturn = objShell.Run(strCommand, 2, True)
If intReturn = 1 Then

WshNetwork.AddWindowsPrinterConnection \\PrintServer01\Printer1
WshNetwork.SetDefaultPrinter \\PrintServer01\Printer1

End If

strCommand = "%comspec% /c N:\IfMember.exe SecondFloor"
intReturn = objShell.Run(strCommand, 2, True)
If intReturn = 1 Then

WshNetwork.AddWindowsPrinterConnection \\PrintServer01\Printer2
WshNetwork.SetDefaultPrinter \\PrintServer01\Printer2

End If


WshNetwork.RemoveNetworkDrive "N:"


The above logon vbs script works great but you may encounter and issue with Windows 7 computers.


For Windows 7 use the following in a batch file:

@echo off


rundll32 printui.dll,PrintUIEntry /q /ga /n \\dc01\Printer-duplex

exit


The above will add a printer at the machine level quietly - No user messages. If run without the quiet switch, the user will will see a message indicating the printer already exists upon logon. The first user will not see the message of course when the entry is first added to the logon script. I used a .cmd file.

 PRINTUI.EXE can be used instead of  RUNDLL32 PRINTUI.DLL,PrintUIEntry  on Windows 7 but then you'll have to add some conditional code to determine if the user is loging in from a windows 7 computer or not.


Create Network Printers with a Logon script batch (.bat) file or cmd file


RUNDLL32 PRINTUI.DLL command line options / switches

Usage:


RUNDLL32 PRINTUI.DLL,PrintUIEntry [ options ] [ @commandfile ]/a[file] binary file name

/b[name] base printer name

/c[name] unc machine name if the action is on a remote machine

/dl delete local printer

/dn delete network printer connection

/dd delete printer driver

/e display printing preferences

/f[file] either inf file or output file

/ga add per machine printer connections

/ge enum per machine printer connections

/gd delete per machine printer connections

/h[arch] driver architecture, one of the following:

Alpha
Intel
Mips
PowerPC

/ia install printer driver using inf file

/id install printer driver using add printer driver wizard

/if install printer using inf file

/ii install printer using add printer wizard with an inf file

/il install printer using add printer wizard

/in add network printer connection

/j[provider] print provider name

/k print test page to specified printer, cannot be combined with command when installing a printer

/l[path] printer driver source path

/m[model] printer driver model name

/n[name] printer name

/o display printer queue view

/p display printer properties

/q quiet mode, do not display error messages

/r[port] port name

/s display server properties

/Ss Store printer settings into a file

/Sr Restore printer settings from a file



Store or restore printer settings option flags that must be placed at the end of command:

2 PRINTER_INFO_2

7 PRINTER_INFO_7

c Color Profile

d PrinterData

s Security descriptor

g Global DevMode

m Minimal settings

u User DevMode

r Resolve name conflicts

f Force name

p Resolve port

/u use the existing printer driver if it's already installed

/t[#] zero based index page to start on

/v[version] driver version, one of the following:

Windows 95 or 98
Windows NT 3.1
Windows NT 3.5 or 3.51
Windows NT 3.51
Windows NT 4.0
Windows NT 4.0 or 2000
Windows 2000

/w prompt the user for a driver if specified driver is not found in the inf

/y set printer as the default

/Xg get printer settings

/Xs set printer settings

/z do not auto share this printer

/Z share this printer, can only be used with the /if option

/? help this message

@[file] command line argument file