Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
I have moved all of our printers from our old print server cmi-25 to our new print server nsfcmi-4. We are currently still using the old server but i want to repoint everyone to the new server by just changing the servername on the current mapping. The share names are all still the same but the server has changed....
For example \\cmi-25\HP_3600 is now \\nsfcmi-4\HP_3600
I found this script below but i keep getting an error on line 17 saying undefined variable [$PriMapState]!
Code:
If NOT @LOGONMODE
Break On
Else
Break Off
EndIf
Dim $SO
$SO=SetOption("Explicit", "ON")
$SO=SetOption("NoMacrosInStrings", "ON")
$SO=SetOption("NoVarsInStrings", "ON")
$SO=SetOption("WrapAtEOL", "ON")
Dim $OldServer, $NewServer, $ArrPtrs, $Ptr, $PtrMapState, $NUL
$OldServer = "\\cmi-25"
$NewServer = "\\nsfcmi-4"
$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs
$PtrMapState = $PriMapState("\\" + $OldServer + "\" + $Ptr)
If 0 < $PtrMapState
$NUL = AddPrinterConnection("\\" + $NewServer + "\" $Ptr)
If NOT @ERROR AND 2 = $PtrMapState
$NUL = SetDefaultPrinter("\\" + $NewServer + "\" $Ptr)
If @ERROR
? "Error setting default Printer Connection " + $Ptr
? "Error " + @ERROR + ": " + @SERROR
EndIf
Else
? "Error adding Printer Connection " + $Ptr
? "Error " + @ERROR + ": " + @SERROR
EndIf
If NOT @ERROR
$NUL = DelPrinterConnection($Ptr)
If @ERROR
? "Error deleting Printer Connection " + $Ptr
? "Error " + @ERROR + ": " + @SERROR
EndIf
EndIf
EndIf
Next
If anyone can suggest a fix or help that would be great... Thanks.....
Edited by Benny69 (2009-05-1401:39 PM) Edit Reason: Added Code Tags
I think the reason your getting that error is because this script is expecting to have a UDF in it called PriMapState, you can find it here. If you copy it into your script I think your error will go away.
Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Wow what a helpful bunch of Moderators you guys are.... That fixed the error in the script but then when i tested the changes were not made by the script. Printers still showing mapped to old server.....
Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Tried your new one as below but getting error on line 34 saying undefined variable [$oldPSvr]!
Code:
; PrintMigrate - Migrates printers from one server to another
; Glenn Barnas - 2006/04/14
;
; Enumerates all printers, moving printers mapped on the old server to the new server
; assumes that old and new printer names are the same. The default printer is preserved.
; Only one print server (per pass) can be migrated with this utility
;
; The old and new print servers must be defined below
;
; If the old and new printer names are not the same, or printers are being moved between
; multiple print servers, use PrintMigrate2, which utilizes a lookup table to map the old
; server\printer to new server\printer. PrintMigrate2 utilizes a similar process to
; PrintMigrate, but is slower due to the 1:1 mapping needed.
Break On
Dim $
Dim $OldPServer, $NewPServer ; old/new print server names
Dim $LPKey ; Key where printers are defined
Dim $DefPtr ; UNC of default printer
Dim $Value ; value read from registry
Dim $I ; index pointer
Dim $Tag ; flag indicating an error in delete/map process
$ = SetOption('Explicit', 'On')
$ = SetOption('WrapAtEOL', 'On')
$ = SetOption('NoVarsInStrings', 'On')
$ = SetOption('NoMacrosInStrings', 'On')
;##############################################################
; Define old & new print servers
$OldPSvr = '\\cmi-25'
$NewPSvr = '\\nsfcmi-4'
;##############################################################
; define key where printers are defined
$LPKey = 'HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts'
; Get current default printer
$DefPtr = ReadValue('HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows', 'Device')
; Display the default printer
If $DefPtr
'Default printer is ' Split($DefPtr, ',')[0] ? ?
Else
'Default printer is not defined.' ? ?
EndIf
; Enumerate the printers defined for this user
$I = 0 ; set the index to 0
; get the first registry value
$Value = EnumValue($LPKey, $I)
While Not @ERROR ; process until an EOD error
'Checking ' $Value
$Tag = 0 ; clear the error tag
If InStr($Value, $OldPSvr) ; If it references the old server
$Printer = Join(Split($Value, $OldPSvr), '') ; get the printer name
DelPrinterConnection($Value) ; delete the current printer connection
If Not @ERROR ; handle error
' - deleted'
Else
' Failed to delete printer!' ?
$Tag = 1
EndIf
If Not $Tag ; add if delete was successful
AddPrinterConnection($NewPSvr + $Printer)
If Not @ERROR
', added'
Else
' Failed to add printer!' ?
$Tag = 1
EndIf
If Not $Tag
If InStr($DefPtr, $Printer)
SetDefaultPrinter($NewPSvr + $Printer)
If Not @ERROR
', Set as default'
Else
' Failed to set as default printer!' ?
EndIf
EndIf
EndIf
?
EndIf
Else
' - OK' ?
EndIf
$I = $I + 1
$Value = EnumValue($LPKey, $I)
Loop
Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
See attached file for error output. Seems to start ok as works out default printer and the first few in the list but then fails and doesnt change any of the mapping to point to the new loaction ???
Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Hooray, it works.... BUT.... it doesnt do all the printers in my list. We have some Rocih printers that do not seem to map over. Is there anything that would stop this working... name length, printer type.....
Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Right, it seems that it just changes a few printers each time it runs. I ran it again and a few more were changed and then the final time it changed the last few printers....
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I'll take a look - when I ran it, it did not have Explicit defined - thought I caught all the DIMs.
As for multiple iterations, that's strange. It only addresses currently mapped printers, and we generally have only 3-4 per computer, so might not have seen that issue.
Glenn
_________________________ Actually I am a Rocket Scientist!