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.
  • Posts

    • yeah GSMA began working to enable end to end encryption between android and iphone last year and apparently a new standard was developed. apple has said that they would implement this in "future software updates" but i haven't heard anything since march, the time this was all reported on. shortly after, i read on forbes that the FBI suggests not sending texts between iphone and android because they're unencrypted. i use signal to chat with my wife but i'd rather just use messages tbh (she has an iphone), i'm not really a 3rd party guy haha
    • Well, I did not like the trailer for the project he went to work on also, but why do you think he should waste time with this… did you love the season 2? Maybe I am missing out after the crap I saw in first season ep1-3? I love the first last of us game… while not the BEST it was one of the games that I will remember for the EXPERIENCE it game me… last of us 2 was not on the same level at all and the show🤔 complete miss in my experience of the first few level
    • They're likely moving all resources to other things. Clearly Windows is not important to them.
    • Image Uploader 1.4.3 Build 5352 by Razvan Serea Image Uploader is a free and open-source program for Windows that that allows you to effortlessly upload images, screenshots, and various files to a wide array of hosting services. With its capability to capture selected screen areas, it promptly uploads content to image hosting services, while also offering the convenience of automatically copying the URL to your clipboard. Key Features of Image Uploader: Upload to Multiple Hosting Services Image Uploader supports uploading images and files to over 30 popular hosting services. Additionally, it can upload directly to your own FTP, SFTP, or WebDAV server. After upload, the tool automatically generates sharing codes in HTML, BBCode, and Markdown, with support for custom output templates tailored to your needs. Video Frame Grabbing and Screenshot Tools You can extract multiple frames from video files in a wide range of formats including AVI, MP4, MKV, WMV, and more. It supports both system-installed codecs and built-in ones. The extracted frames can be uploaded individually or compiled into a single mosaic image. It also includes screenshot capabilities for the full screen or selected regions, along with a simple image editor for annotations, highlights, and blurring. Advanced Integration and Usability Image Uploader supports drag-and-drop, clipboard monitoring, and can be accessed via Windows Explorer’s context menu. It also features URL shortening, multi-account support, reuploading, and the ability to upload images embedded in text while retaining formatting. The app is available in several languages, including English, Russian, Turkish, Korean, Arabic, and more. Image Uploader 1.4.3 Build 5352 changelog: New Features Screen Recording: Added two powerful capture methods: DirectX (Desktop Duplication API) FFmpeg-based recording Expanded Hosting Services: Added support for new file hosting platforms: TeleBox (linkbox.to) take-me-to.space ranoz.gg webshare.cz lobfile.com imgpx.com freeimghost.net radikal.cloud anonpic.org fotozavr.ru imgtr.ee thumbsnap.com 8upload.com filemail.com Others Video Uploads: Added Flickr.com support for video uploads Localization: New French translation added Context Menu: Added "File Information" option to video file context menus DPI Support: Improved support for: Screen DPI changes Mixed-DPI multi-monitor setups Improvements Disabled application window animations during screenshot/screen recording initiation Updated API and documentation Improved overall stability Bug Fixes Fixed network client error that could cause application crashes Resolved unauthorized startup registration issue Fixed upload functionality for pixeldrain.com Restored tray icon balloon notifications visibility Various minor bug fixes Download: Image Uploader 64-bit | Portable 64-bit | ~16.0 MB | (Open Source) Download: Image Uploader 32-bit | Portable 32-bit | ~15.0 MB Download: Image Uploader ARM64 | Portable ARM64 | ~11.0 MB Links: Image Uploader Home Page | Screenshot | GitHub Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • A new Forza was among the things specifically mentioned as 'next year' during the Showcase in June. That's more than likely Horizon 6. Turn 10 made Motorsport and is responsible for the engine behind Horizon and the Fable reboot (with adjustments for each, of course). I think they'll likely remain a support studio for Playground. Given the fact that Turn 10's staff has been halved, and how much of a fumble the launch of Motorsport was (which remember, they initially branded as a 'platform' rather than just a standalone thing), the transition to support studio wouldn't surprise me.
  • Popular Contributors

    1. 1
      +primortal
      431
    2. 2
      ATLien_0
      156
    3. 3
      +FloatingFatMan
      148
    4. 4
      Nick H.
      64
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!