Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Migrating users printers

Status
Not open for further replies.

bilbonvidia

Technical User
Feb 6, 2007
131
GB
Hi,

I have installed the network printers on to a new print server with a different name using the handy microsoft print migrate tool.

Is there an easy way I can change users printers to point at the new server using group policy or a simple script without having to do it manually on each computer?
 
I did it via a logon script.

create a bat file with the following in it:
Code:
cscript \\server\share\printer_change\printer_change.vbs \\server\share\printer_change\changes.txt //NoLogo

it calls the actual scipt, and uses the list in the changes.txt file, which uses the format
Code:
\\old_server\printer1;\\new_server\printer1

The scipt will look for all the printers on the list, and replace them with the new server. It will also retain the default printer.

Code:
On Error Resume Next
Const VERBOSE = "N"   'Display progress (Y or N)
Const Title = "Print Connection Migrator"
Const ForReading = 1

Dim strDefaultPrinter 
Dim InstalledPrinters 'Array of printer names
Dim Textfile          'File with print Q names
Dim OldPrintQueues()  'Dynamic array to store old print queue names, from the text file
Dim NewPrintQueues()  'Dynamic array to store new print queue names, from the text file
Dim fso         'File System Object
Dim objTextFile 'Text file object
Dim strNextLine 'Line of the text file
Dim i
Dim WshNetwork

Set WshNetwork = CreateObject("WScript.Network")

' 1. - Get the command line args        ###
SET Parameters = Wscript.arguments

'If no command line arguments provided, quit
If Parameters.Count = 0 Then
    WScript.Quit(1)
Else
    Textfile = Parameters.item(0)
End If

If Textfile = "" or Not Right(TextFile,4) = ".txt" or Not FileExist(Textfile) Then
    Error=MsgBox("No valid input file provided. Stopping the script now.",vbokonly, Title)
    WScript.Quit(1)
End If

If VERBOSE = "Y" Then Wscript.Echo "Reading input file"
' 2. Read the text file and import it in a Array    ###
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (TextFile, ForReading)
i=-1

While not objTextFile.AtEndOfStream
    i = i+1
    Redim Preserve OldPrintQueues(i)
    ReDim Preserve NewPrintQueues(i)
    strLine = objTextFile.Readline
    'Do not import the comment lines
    If Left(strLine,2) = "\\" Then
        OldPrintQueues(i) = Left(strLine,InStr(strline,";")-1)
        If VERBOSE = "Y" Then Wscript.Echo " old Q is: " & OldPrintQueues(i)
        NewPrintQueues(i) = Mid(strline,InStr(strline,";")+1,Len(strline))
        If VERBOSE = "Y" Then Wscript.Echo " new Q is: " & NewPrintQueues(i)
    End If
Wend

objTextFile.Close

' 3. Store the name of the default Printer        ###

strDefaultPrinter = DefaultPrinter
If VERBOSE = "Y" Then Wscript.Echo " Default is: " & strDefaultPrinter

' 4. WMI query for current printer connections    ###

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")

' 5. Main Loop through printer collection         ###

For Each objPrinter in colInstalledPrinters
    If VERBOSE = "Y" Then Wscript.Echo " Current connection: " & objPrinter.Name
    If Left(objPrinter.Name, 2) = "\\" Then 'Work only On network printers
        'Search the corresponding printer and create it
        i = 0 'set the indice at the beginning of the array (prepare to loop)
        Do Until i > UBound(OldPrintQueues)
            If UCase(objPrinter.Name) = UCase(OldPrintQueues(i)) Then
                'Create the connection to the new printer
                If VERBOSE = "Y" Then Wscript.Echo " New connection: " & NewPrintQueues(i)
                WshNetwork.AddWindowsPrinterConnection NewPrintQueues(i)
                If UCase(strDefaultPrinter) = UCase(objPrinter.Name) Then 'This is the default printer
                    'Set the default Printer
                    WshNetwork.SetDefaultPrinter NewPrintQueues(i)
                End If
                'Delete the printer connection
                WshNetwork.RemovePrinterConnection objPrinter.Name
                If VERBOSE = "Y" Then Wscript.Echo " Removing : " & objPrinter.Name           
            End If
            i = i + 1
        Loop
    End If 'End of check for network printers
Next 'End of the loop through the printers of this user

Set WshNetwork = Nothing


'-----------------
' Functions
'-----------------

'Return the default printer
Function DefaultPrinter
    Dim strComputer
    Dim Result
    
    strComputer = "."
    Result = ""
    
    Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters = objWMIService.ExecQuery _
     ("Select * from Win32_Printer")
    For Each objPrinter in colInstalledPrinters
        If objPrinter.Default = True Then
         Result = objPrinter.Name
        End If
    Next
    DefaultPrinter = Result
End Function

'-----------------

'Does File Exist (Boolean)
Function FileExist (FileFullPath)
    Dim Fso
    Set Fso = CreateObject("Scripting.FileSystemObject")
    If (Fso.FileExists(FileFullPath)) Then
        FileExist = True
    Else
        FileExist = False
    End If
End Function

I'll be honest, I didn't write this, but I can't remember where i got it :)

------------------------------------------------------
Matt
Life is all shadows and dust.
Live it up with women and wine while you can
 
Thank you I just tried that on a test pc and it worked perfectly.
 
For another way of doing it using VBScript, check out my login script FAQ.

faq329-5798

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
That stuff is very cool.

What is the local user version of this: WSHNetwork.username
?

Regards
Bill

 
What is the local user version of this: WSHNetwork.username

Same thing for domain or standalone PC, just use WSHNetwork.username

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
why would I get this error:

object required:'WSHNetwork'

<code>

'=======================================================
' Determine the appropriate greeting for the time of day.
'=======================================================
Dim HourNow, Greeting
HourNow = Hour(Now)
If HourNow >5 And HourNow <12 Then
Greeting = "Good Morning "
Elseif HourNow =>12 And HourNow <16 Then
Greeting = "Good Afternoon "
Else
Greeting = "Good Evening "
End If

Dim oVo
Set oVo = Wscript.CreateObject("SAPI.SpVoice")
oVo.speak Greeting & WshNetwork.UserName

</code>

 
Because you are grabbing my add on code out of context. The Add on code in my login script FAQ is designed to be plugged into my login script in the section indicated.

Above that point in my script I create the WSHNetwork object.

You need to have:

Set WSHNetwork = CreateObject("Wscript.Network")
at the top of your script.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Cheers Mark.

Can you recommend any good websites to learn vbs for a relative newbie?

Bill
 
Microsoft.com/scripting has a lot of great information.

Also as a reference I like DevGuru.

For instruction you could try W3Schools.com.

And the FAQs in this forum are awesome.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top