VBScript to add network printer at logon (Vista)


Recommended Posts

Is there a specific VBScript to add printers at logon for Windows Vista clients?

The script i constructed seems to great under XP but users are constantly complaining printers are missing, these users are on Vista.

I've been messing for a few hours and got it to ask to install the printer driver at logon but it still didn't add the printer.

I use something like this:

' Read security group membership
 adsDomainGroups.Filter = Array("Group")
 For Each adsGroup in adsDomainGroups
  Group = UCase(adsgroup.name)

 UserName = UCase(WshNetwork.UserName)
 server = "\\server" 'Change to match the users server
home = "W:" ' Put the users home drive letter here.

  If group = UCASE("staff") Then CheckGroup adsgroup.name, Username, home, server & "\staff$\" & UserName, "staff"
  Next
 ' Maps network drives
	 WshNetwork.MapNetworkDrive "x:", server & "\shared"
	 WshNetwork.MapNetworkDrive "s:", server & "\software"
	 WshNetwork.MapNetworkDrive "y:", server & "\Childrens Shared"

 ' Adds classroom printers
		 PrinterPath1 = server & "\ICT Suite"
		 PrinterDriver1 = "Generic 35C-4 Series PS"
		 Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
		 Wshnetwork.setdefaultprinter server & "\ICT Suite"

this is why I don't like using vbscripts. they work great in the fashion that you are used to, however something changes in the way a script is handled (common in os changes) you end up rewriting the script or the lines that are buggy.

http://www.computerperformance.co.uk/ezine/ezine16.htm

that should take care of your issue. basically use the unc path statement.

Thanks for the web link.

The following script:

Option Explicit
Dim netPrinter, UNCpath
UNCpath = "\\server\ICTSuite"
Set netPrinter = CreateObject("WScript.Network")
netPrinter.AddWindowsPrinterConnection UNCpath
WScript.Echo "Your printer is mapped from : " & UNCpath
WScript.Quit

generated an error.. The data is invalid. 8007000D (null)

  Lilrichie said:
Set WshNetwork = CreateObject("WScript.Network")
 WshNetwork.AddWindowsPrinterConnection "\\printerserver\printername"
 WshNetwork.SetDefaultPrinter "\\printerserver\printername"

Would this not work under Vista then?

Well it's not working for me. :( I am trying this:

If group = UCASE("staff") Then CheckGroup adsgroup.name, Username, home, server & "\staff$\" & UserName, "staff"
 Next
' Maps network drives
	WshNetwork.MapNetworkDrive "x:", server & "\shared"
	WshNetwork.MapNetworkDrive "s:", server & "\software"
	WshNetwork.MapNetworkDrive "y:", server & "\Childrens Shared"

' Maps network printers
	WshNetwork.AddWindowsPrinterConnection "\\server\ICTSuite"
	WshNetwork.SetDefaultPrinter "\\server\ICTSuite"

The network drives are mapping... but no sign of the printer.

  forcer said:
Well it's not working for me. :( I am trying this:

If group = UCASE("staff") Then CheckGroup adsgroup.name, Username, home, server & "\staff$\" & UserName, "staff"
 Next
' Maps network drives
	WshNetwork.MapNetworkDrive "x:", server & "\shared"
	WshNetwork.MapNetworkDrive "s:", server & "\software"
	WshNetwork.MapNetworkDrive "y:", server & "\Childrens Shared"

' Maps network printers
		Set WshNetwork = CreateObject("WScript.Network") 'this tells the script what you want to do
	WshNetwork.AddWindowsPrinterConnection "\\server\ICTSuite"
	WshNetwork.SetDefaultPrinter "\\server\ICTSuite"

The network drives are mapping... but no sign of the printer.

I think you have missed a line out (try above)

  Lilrichie said:
I think you have missed a line out (try above)

I have that at the top of the script

Public menutype, obj, WshNetwork, fs, adsDomainGroups
Set obj = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network") '<---here
domain = WshNetwork.UserDomain
Set adsDomainGroups = GetObject("WinNT://" & domain)
' Set fs = CreateObject("Scripting.FileSystemObject")

After looking around it appears most of the problems are caused by UAC ... you can't run VBS or modify the registry. There are lots of problems online with people not being able to run scripts under Vista.

To get around this it requires some editing in task scheduler to elevate scripts or running a reg script locally which will add the ability to run vbscripts as administrator... but this means i have to go round each computer and do it manually.

This has lead me to believe that there must be another way to assign printers in a network environment? if there isn't, why would Microsoft implement this and make it almost impossible to install printers with UAC enabled, when they highly recommend you don't disable it?

I have pretty much solved the issue, here is the info for anyone else that might be having problems, someone may find it useful

First of all i decided to split everything into location, this way i could set the default printer to the closest printer for that area as we have 3 different printers around the building. So i added a 'room' key to the registry.

HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\

REG_SZ "room" .. value "AREANAME"

i then created the following to read the registry key:

  Quote
Public obj, WshNetwork

Set obj = CreateObject("WScript.Shell")

server = "\\server" 'Change to match the users server

' Reads the room registry key

room = obj.regread("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\room")

' Reads the room key and if it = "SERVER" stops the script running. Put the word "SERVER" in all servers room keys

If room = UCase ("SERVER") Then

Msgbox "This is the " & room & "." & vbTab & "Stopping the script" & vbCr & "Right click the script and select edit"

WScript.quit

Else

End If

now to assign the printers to each area:

	If room = "AREANAME" Then

		 PrinterPath1 = server & "\Printername"
		 PrinterPath2 = server & "\Printername2"
		 PrinterDriver1 = "Name of Printer"
		 PrinterDriver2 = "Name of Printer"
		 Wshnetwork.addwindowsprinterconnection Printerpath1, Printerdriver1
		 Wshnetwork.addwindowsprinterconnection Printerpath2, Printerdriver2
		 Wshnetwork.setdefaultprinter server & "\Printername"

	 Else
	 End If

continue that code for the amount of area's you have assigned. This way you can set default printers for different area's

the first time the computer is logged on it will ask you to install the printer drivers so you will need admin credentials... it only asks once and printers get mapped after every other logon. You still need to go round each computer manually but at least they are being mapped.

The If room = UCase("SERVER") statement should amended to one of the below...

"SERVER" is already in uppercase so no need to UCase it. The variable you are reading in "room" may be in mixed case therefore you need to UCase that to ensure the IF statement captures it.

' Reads the room registry key
room = obj.regread("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\room")

' Reads the room key and if it = "SERVER" stops the script running. Put the word "SERVER" in all servers room keys
If UCase(room) = "SERVER" Then

or

' Reads the room registry key
room = UCase(obj.regread("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\room"))

' Reads the room key and if it = "SERVER" stops the script running. Put the word "SERVER" in all servers room keys
If room = "SERVER" Then

  • 2 weeks later...

My printer vbs script looks like this

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, strUNCPrinter, objNetwork

Set WshNetwork = WScript.CreateObject("WScript.Network")
On Error Resume Next
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
PrinterPath = "\\MYSERVER\PRINTERNAME"
PrinterExists = False
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
	If WSHPrinters.Item(LOOP_COUNTER +1) = PrinterPath Then
	  PrinterExists = True
	End If
Next
If PrinterExists = 0 Then
  WshNetwork.AddWindowsPrinterConnection "\\MYSERVER\PRINTERNAME"
End If

If you change "PrinterExists = -1" that means if the printer is already connected then run a different command like remove the printer (say it's an old printer that you don't want any of your clients to see anymore).

This script works for both XP/Vista machines on my domain. I have the end of my log on script execute the printer check.vbs so that way every time a person logs in, it doesn't always assume a printer is connected and just keep reinstalling it.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.