We help IT Professionals succeed at work.

Script to assign a network printer

Bianchi928
Bianchi928 asked
on
378 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
*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 ?

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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.

Author

Commented:
Top

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.