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
VB ScriptProgramming

Avatar of undefined
Last Comment
markdmac

8/22/2022 - Mon
RobSampson

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

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
Bianchi928

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bianchi928

ASKER
Bianchi928

ASKER
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
RobSampson

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
RobSampson

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.
Bianchi928

ASKER
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
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bianchi928

ASKER
*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 ?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bianchi928

ASKER
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
RobSampson

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.
Bianchi928

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bianchi928

ASKER
Top
markdmac

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