Script to Delete Old Mappings to Print Server with Share Name Like

OK here if my problem and it is one that I got myself into.... I am in the process of migrating our Windows 2008 printer cluster to 2008 R2 which host 600+ printers. All printers are deployed via GPP. So I updated each existing GPP for each printer to point to the new cluster and everything worked perfectly. I thought that it would change the current mapping to the new printer server. What it did is that is created another instance of the printer on the new cluster so now user's have duplicate mappings...

I know what you are thinking testing?? Well this is one item I over looked .

I know GPP can remove a printer but it needs the full UNC path name... I can do this for 600+ printer but this not very efficient.

Online I found a script someone wrote that deletes all printers that are connected to a printer serve which is great but I need to remove printer mappings in a more controlled fashions since some printers were removed.

I need a script that will delete all printers on a server with a share name that starts with .. Here's an example...

\\server\HP 43 or \\server\Room 123 H or \\server\123

All clients are Windows 7 I am not a scripting person so I turn to the experts for help
LVL 20
compdigit44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

compdigit44Author Commented:
Here is a link to a script that will remove all printers from a server. Could I edit this to make it more refined and delete all printer that match a certain naming scheme?

http://community.spiceworks.com/scripts/show/632-remove-all-network-printers-by-print-server-name
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Hello compdigit44 - you could but I'm thinking more of a basic dos batch file that will do the same, this would probably be easier/simpler.

Something like this:

for /f "tokens=3 delims= " %a in ('net use ^| findstr /i \\\\server') do @echo net use "%a" /d

Open in new window


Can you try the above command on a cmd prompt? It should echo the connected \\server\printers - if it works, remove the echo (after the @). If you want to put it into a batch file, all %a must be %%a
0
Robberbaron (robr)Commented:
also try this vbs version that may be easier to include in logon script
         Set WshNetwork = WScript.CreateObject("WScript.Network")

         Set oPrinters = WshNetwork.EnumPrinterConnections
	 dim srvName 
	srvName = "\\SUSIE-PC\"
         WScript.Echo 
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
	    if left(oPrinters.Item(i+1),len(srvName))=srvName then
           		WScript.Echo "Delete#" & i & "=Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
                      'objNetwork.RemovePrinterConnection oPrinters.Item(i+1)
            end if
         Next

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

compdigit44Author Commented:
Everyone thanks for the help. I will be trying this first thing tomorrow morning.  DO either script delete a printer is the share name starts with something. Also can the script handle spaces in the share name
0
Gerwin Jansen, EE MVETopic Advisor Commented:
net use will not handle your printers with a space in the name properly (why do people do that btw).

Can you show the output this reg query:

reg query HKCU\printers
0
compdigit44Author Commented:
The printers where name this way long before I state at my company 6 months ago....

A sample of HKCU\printers looks like the following

\\ServerA\HP LaserJet 4350 RM33 PCL6
\\ServerA\Ricoh 443
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Ok, try this then instead:

for /f "tokens=* delims= " %%a in ('reg query HKCU\printers ^| findstr /i \\\\servera') do echo net use "%%a" /d

Using at test file containing your sample HKCU\printers I got this from a command line test:

C:\Temp>for /f "tokens=* delims= " %a in ('type test.txt ^| findstr /i \\\\servera') do echo net use "%a" /d

C:\Temp>echo net use "\\ServerA\HP LaserJet 4350 RM33 PCL6" /d
net use "\\ServerA\HP LaserJet 4350 RM33 PCL6" /d

C:\Temp>echo net use "\\ServerA\Ricoh 443 " /d
net use "\\ServerA\Ricoh 443 " /d

Open in new window

0
compdigit44Author Commented:
Thanks but the problem is that I have 20 or 30 types of printers on my network
0
compdigit44Author Commented:
From the link I posted earlier would it work if I took the line and did the following....

PrintServer = "SERVERNAME" 'Your Print server name goes here

PrintServer = "\\ServerA\HP 43*" (* means anthing following.

From what I have read about win32_print I would need to us / call ServerName and ShareName


Please Help!!!!!! :o(
0
Robberbaron (robr)Commented:
1. Yes just put your server names there.
2. Yes any prefix will work incl portion of the share name.

Just try it as the delete portion currently commented out. Run from cscript to avoid all the prompts.

my test.... first with srvName="\\VENUS\"  ie the printer server
then with \\VENUS\CHAND... only printers on VENUS that start with CHAND
H:\>cd ee

H:\EE>cscript prt-del.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Network printer mappings:
Delete#8=Port 192.168.1.96 = \\VENUS\Xerox 6279 Wide Format HPGL
Delete#10=Port 192.168.1.92_1 = \\VENUS\CHANDLER Left_v1
Delete#14=Port 192.168.1.91 = \\VENUS\MONICA C364 Right
Delete#16=Port 192.168.1.92 = \\VENUS\CHANDLER C364 Left
Delete#18=Port 192.168.1.92 = \\VENUS\CHANDLER C364 PS_v1

H:\EE>cscript prt_del.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.


Network printer mappings:
Delete#10=Port 192.168.1.92_1 = \\VENUS\CHANDLER Left_v1
Delete#16=Port 192.168.1.92 = \\VENUS\CHANDLER C364 Left
Delete#18=Port 192.168.1.92 = \\VENUS\CHANDLER C364 PS_v1

H:\EE>

         Set WshNetwork = WScript.CreateObject("WScript.Network")

         Set oPrinters = WshNetwork.EnumPrinterConnections
	 dim srvName 
	srvName = "\\VENUS\CHANDLER Le"
         WScript.Echo 
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
	    if left(oPrinters.Item(i+1),len(srvName))=srvName then
           		WScript.Echo "Delete#" & i & "=Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
                      WshNetwork.RemovePrinterConnection oPrinters.Item(i+1)
            end if
         Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robberbaron (robr)Commented:
note!!!  i removed the comment mark from the RemovePrinter line 11 above,  

Add it back for your testing.
0
compdigit44Author Commented:
SO far this is working perfectly!!!!

I am doing a couple more test thought just to be sure
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.