Link to home
Create AccountLog in
Avatar of Bianchi928
Bianchi928

asked on

Script to assign a network printer

I have 2 printer servers aupriprint01 & aupriprint02. I need a script that will run locally on each computer . It will use the first 4  characters of computer name and add all printers from the server where the name starts with the 4 first character. It will look on each server.

Eg: Computer Name = u794c1
It will look on both servers for any printer where the name start with u794 and add these printers to the computer
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi there,

I think this will do it.

Regards,

Rob.
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objNetwork = CreateObject("WScript.Network")

arrServers = Array("aupriprint01", "aupriprint02")
For Each strServer In arrServers
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT ShareName FROM Win32_Printer WHERE ShareName LIKE '" & Left(objNetwork.ComputerName, 4) & "%'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	For Each objItem In colItems
		objNetwork.AddWindowsPrinterConnection "\\" & strServer & "\" & objItem.ShareName
	Next
Next

Open in new window

Avatar of Bianchi928
Bianchi928

ASKER

Hi Rob !!

You're always up to the task. I will test it later and let you know. Can you please have a look at my previous question regarding migrating printers from one server to another cuz I don't want to use MIgrator for that.

Thanks
Cheers
Helllo,

I've just tested your script and I'm getting an error message on Line 9..( iLine 8 in your script cuz I added a blank line in mine). Look at screendump1

Next I did a change and added strcomputer = "u204c1"  and I got an error message too. Look at screendump2.

I forgot to mention that the retail outlet are running TS.


screendump1.JPG
I have tried a few more things....I've modified the script to ...

strcomputer = "."
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objNetwork = CreateObject("WScript.Network")

arrServers = Array("aupriprint01", "aupriprint02")
For Each strServer In arrServers
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
      Set colItems = objWMIService.ExecQuery("SELECT Sharename FROM Win32_Printer WHERE ShareName LIKE '" & Left(objNetwork.ComputerName, 4) & "%'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
        wscript.echo strserver
        wscript.echo objNetwork.ComputerName
        wscript.echo ShareName
      For Each objItem In colItems
            objNetwork.AddWindowsPrinterConnection "\\" & strServer & "\" & objItem.ShareName
      Next
Next

And no sharename is echo. I've attached a screenshot of the print server which shows my computer (617YW1S).

Cheers

printserver.JPG
Apologies for my delay, I'm terribly busy so far this week.  Just a modification to your latest code, I have moved the ShareName echo to inside the loop, as that's where it would exist, and used objItem.ShareName instead.  See if that gives you any output.

Regards,

Rob.
strcomputer = "."
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objNetwork = CreateObject("WScript.Network")

arrServers = Array("aupriprint01", "aupriprint02")
For Each strServer In arrServers
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT Sharename FROM Win32_Printer WHERE ShareName LIKE '" & Left(objNetwork.ComputerName, 4) & "%'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	wscript.echo strserver
	wscript.echo objNetwork.ComputerName
	For Each objItem In colItems
		wscript.echo objItem.ShareName
		objNetwork.AddWindowsPrinterConnection "\\" & strServer & "\" & objItem.ShareName
	Next
Next

Open in new window

Also, in the screenshot you have above, for each printer, is the "ShareName" the same as the Name?  For whichever one of those attributes contains the workstation name, change
WHERE ShareName LIKE
accordingly.  So if it's actually the "name" that contains the workstation name, and not the "sharename", change that to
WHERE Name LIKE

Regards,

Rob.
Hi Rob,

I completely understand that you're busy. We are also having some problems with receiving external emails. So, I had to go to the EE website to see if there was any feedback . Okay I tested your latest script and it still doesn't show the sharename. I get the computer & server name and that's it. I've attached a screenshot of the printer sharenames

Cheers

screenshot.JPG
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
*haha*..Even happens to the master...oops Genius..It work fine now Rob. Even with my little knowledge of VBScript I should have pick this up.

The only thing is that it takes time to add the printers. Is there any way to speed it up ?
As per the attachment, it's all working fine. I forgot to ask you how to remove the "pos" printer before adding the new ones (refer to the attachment) . If I need to raise a new question just let me know.
screenshot.JPG
Not really.  The WMI query will take time to enumerate all of the objects (it has to to be able to get a subset of the required names), and then the installation of the driver (for the first connection) will also take time.  Not much we can do I'm afraid.

Regards,

Rob.
That's ok Rob. Can you please have a look at  ID : 35820560 . I need to delete these "pos" printers before adding the new ones.
Top
Bianchi928, take a look at my login script FAQ which has code to replace printers as you request when moving from one server to another.

http://www.tek-tips.com/faqs.cfm?fid=5798