print migration script

M

Martin

Im trying to use the script below but I get an error
saying:
Script: \\server\share\scriptname.vbs
Line: 5
Char: 1
Error: Expected statement
Code: 800A400
Source Microsoft VBScript compilator error

I have made the file translateprinter.txt and put it on in
a share on the server with the only line \\server\printer
in it and changed the script to use that file. Do I need
to change anything else in this script?
Thanks alot
Martin

'==========================================================
=
' NAME: W2kChgPrint.vbs
' COMMENT: This is a sample script
' For the purpose of updating W2K and WinXP
clients UNC printers
to a new
UNC
' printer path.
' This script is designed to handle the following
format only
' <old UNC printer> <new UNC printer>
' \\server1\shareA \\server2\shareC
' \\server1\shareB \\serverX\ShareB
'
' NOTES:
' Do not add blank spaces. Both original and new UNC
printer path must be
included.
' One tab only between original UNC printer path and new
UNC printer path
' The line in the script "TranslateFile =..." should be
updated
'========================================================
Option Explicit
'On error Resume Next

Dim TranslateFile 'File that stores the old printer path
and new printer
path
TranslateFile = "\\systemname\W2K-
ChgPrint\translateprinter.txt"
Dim dict 'The dictionary object, dict.item()
points to the new printer
Dim Key 'The dictionary key to each item

Dim PrinterPath 'Contains the Path to the original
printer, ie
\\server\share
Dim objLocator, objService, objEnumerator
Dim objWSH, strComputerName

Set objWSH = CreateObject("WScript.Network")
strComputerName = objWSH.ComputerName

set objLocator = CreateObject("WbemScripting.SWbemLocator")
set objService = objLocator.ConnectServer
(strComputerName,"root/cimv2")
set objEnumerator = objService.ExecQuery("SELECT * FROM
win32_Printer")

Set dict = CreateObject("Scripting.Dictionary")
CreateDictionary 'Populate the
dictionary object

For each key in objEnumerator
PrinterPath = Ucase(key.ServerName & "\" &
key.ShareName) 'enumerated
from local system
If dict.Exists (PrinterPath)
Then 'if
PrinterPath matches dictionary item
objWSH.AddWindowsPrinterConnection dict.item
(PrinterPath)'Add printer
in
dictionary that matches PrinterPath
If (ucase(GetDefaultPrinter()) = ucase(key.name)) then
_
objWSH.SetDefaultPrinter dict.item(PrinterPath)
objWSH.RemovePrinterConnection PrinterPath, true,
true 'This line
deletes old
printer
End if
Next

'** This function returns the name of the printer
ie "\\server\HP Laser Jet
2100"
Function GetDefaultPrinter()
Dim RegPath 'Location that stores users
Default Printer
Dim Loc 'Used to locate the comma after
the printer name
Dim DefaultPrinter 'Temporary stores the data read
from RegPath
DIM WSHShell 'Windows Scripting Host object

Set WSHShell = WScript.CreateObject("WScript.Shell")
RegPath = "HKCU\Software\Microsoft\Windows
NT\CurrentVersion\Windows\Device"
GetDefaultPrinter="" 'If we error out reading Default
printer will be
nothing

On Error Resume Next 'Script should continue running
even if registry
data is
not found
DefaultPrinter = WSHShell.RegRead(RegPath)
Loc = instr(3,DefaultPrinter,",",vbTextCompare)
GetDefaultPrinter = Mid(DefaultPrinter,1,loc - 1)
End Function

'** This fuction adds the Translatefile.txt into a
dictionary object
'** The original \\server\share is assigned to the
dictionary Key (like a
database)
'** The destination \\server\share becomes the associated
Item to the Key
'** If you do not like dictionary objects you could use a
2 dimensional
array
instead
Function CreateDictionary()
Dim File, FSO, Line, OrgPrinter, NewPrinter, Loc
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile (TranslateFile, 1)
While not File.AtEndOfStream 'Loop until the
end of the file is
read
Line = Ucase(File.ReadLine) 'Read line of text
from the file
Loc = instr(1,Line,chr(9),vbTextCompare) 'Find the
tab in the line read
if Loc > 3 then 'Do not add line
to dictionary if
Tab not
found
OrgPrinter = Mid(Line,1,Loc -1) 'contents before
tab is the
original
\\server\share
NewPrinter = Mid(Line,Loc +1) 'Contents after
tab is the
destination
\\server\share
dict.Add OrgPrinter, NewPrinter 'adds another
set of "Key, Item"
to the
dictionary
end if
WEnd
End Function
===================================================
 
M

Martin

After help from Rashmi I got this working.
A good text editor is also to recommend.
The only thing that needs to be changed is the file
"TranslateFile" (add a txt-file with the old and new
\\server\share in it) and change location to it in the
script.
Really good script that allows a print server change
without any configuration change on users computers.

Thanks once again to Rashmi
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top