Solved

Script or GPO to unstall Printers and reinstall them

Posted on 2009-07-10
15
186 Views
Last Modified: 2012-05-07
I'm in the process of moving my print server to a new 2008 box.  Is there a way to unstall the printers from all the desktops and reinstall them via a group policy or bat file? Not everyone uses the same printers
0
Comment
Question by:tbonehwd
  • 10
  • 5
15 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 24824486
Paste the script below into a text file with a .vbs extension.  Customize the value of the strOldServer variable on line 1 with the name of the old print server.  Customize the value of the strNewServer variable on line 2 with the name of the new print server.  Running the script will find all printers mapped to the old server and re-map them to the new server using the same share names.

This could be run as a logon script.


strOldServer = "printserver"

strNewServer = "new-printserver"
 

On Error Resume Next
 

Set objNetwork = CreateObject("WScript.Network")

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 

Set colInstalledPrinters = objWMIService.ExecQuery _

    ("Select * From Win32_Printer")
 

For Each objPrinter in colInstalledPrinters

    If LCase(objPrinter.ServerName) = "\\" & LCase(strOldServer) Then

        strNewPrinter = "\\" & strNewServer & "\" & objPrinter.ShareName

        objNetwork.AddWindowsPrinterConnection strNewPrinter

        

        If objPrinter.Default = "True" Then

            objNetwork.SetDefaultPrinter strNewPrinter

        End If

        

        objPrinter.Delete_

    End If

Next

Open in new window

0
 

Author Comment

by:tbonehwd
ID: 24824497
Can you do this via group policy in 2008?
0
 

Author Comment

by:tbonehwd
ID: 24826365
i'm going to try this and I'll let you know. Thanks in advance.
0
 

Author Comment

by:tbonehwd
ID: 24826501
stupid question.  leave the "'s?
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24826591
Yes, leave the quotation marks.
0
 

Author Comment

by:tbonehwd
ID: 24826831
it's not working. I tried running the script on the machine and nothing happens.
0
 

Author Comment

by:tbonehwd
ID: 24826863
This is what I did.
strOldServer = "hcidc02"
strNewServer = "hcidc2"
 
On Error Resume Next
 
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer")
 
For Each objPrinter in colInstalledPrinters
    If LCase(objPrinter.ServerName) = "\\" & LCase(hcidc02) Then
        hcidc2 = "\\" & hcidc2 & "\" & objPrinter.ShareName
        objNetwork.AddWindowsPrinterConnection hcidc2
       
        If objPrinter.Default = "True" Then
            objNetwork.SetDefaultPrinter hcidc2
        End If
       
        objPrinter.Delete_
    End If
Next
0
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
LVL 38

Expert Comment

by:Shift-3
ID: 24826954
No, per the instructions the only lines you should be editing are 1 and 2.


strOldServer = "hcidc02"

strNewServer = "hcidc2"

 

On Error Resume Next

 

Set objNetwork = CreateObject("WScript.Network")

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

 

Set colInstalledPrinters = objWMIService.ExecQuery _

    ("Select * From Win32_Printer")

 

For Each objPrinter in colInstalledPrinters

    If LCase(objPrinter.ServerName) = "\\" & LCase(strOldServer) Then

        strNewPrinter = "\\" & strNewServer & "\" & objPrinter.ShareName

        objNetwork.AddWindowsPrinterConnection strNewPrinter

        

        If objPrinter.Default = "True" Then

            objNetwork.SetDefaultPrinter strNewPrinter

        End If

        

        objPrinter.Delete_

    End If

Next

Open in new window

0
 

Author Comment

by:tbonehwd
ID: 24826961
I tried that origionally.  I'll try again.
0
 

Author Comment

by:tbonehwd
ID: 24826991
Ok that deleted the printers but it didn't install them.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24827146
Are the printers on the new server shared under the same names as on the old server?
0
 

Author Comment

by:tbonehwd
ID: 24827174
No I made sure to make them different.  They are simular but not the same.
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 24827218
Ah, well that will prevent this script from working.  It attempts to re-map them to the same names.
0
 

Author Comment

by:tbonehwd
ID: 24827230
OK If that's all it is I'll rename the shares to match and try that.  i'll get back to you on Monday.  Thanks.
0
 

Author Closing Comment

by:tbonehwd
ID: 31602092
Thanks for your help. The script worked.  I appreciated all your efforts.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now