nige1979
(Fresh Scripter)
2009-05-14 01:30 PM
Changing the Mapped Printers Servername

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.....



Benny69
(MM club member)
2009-05-14 01:39 PM
Re: Changing the Mapped Printers Servername

Hi nige1979 and Welcome to the board.

When you post code please place that code in code tags, here is a link that will help you with that, The Post/Reply Formatting Box and How to use it

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.


nige1979
(Fresh Scripter)
2009-05-14 02:00 PM
Re: Changing the Mapped Printers Servername

Which bit do i copy and where do i paste it on the script. Sorry not very good with this stuff.....

Benny69
(MM club member)
2009-05-14 02:06 PM
Re: Changing the Mapped Printers Servername

Sure no worries, you want to copy the entire UDF (User Defined Function) into your script, just like this:

 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

;FUNCTION	PriMapState 
; 
;AUTHOR		Lonkero (Jooel.Nieminen@gwspikval.com) 
; 
;ACTION		Checks for existent networkprinter connection 
; 
;VERSION	1.1.1 
; 
;CHANGES	1.1.1	- 01. november 2003 
;		 Fixed buggie descriped in: 
;		 http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=1;t=008079 
;		1.1	- 02. July 2002 
;		 added support for win9x 
;		1.0	- 01. July 2002 
;		 initial release 
; 
;SYNTAX		PriMapState(PRINTER) 
; 
;PARAMETERS	PRINTER 
;		 to be checked Printer's name 
; 
;RETURNS	1 if printer connected 
;		2 if printer is default 
;		nothing if not connected 
; 
;REMARKS	code for w9x adapted from BrianTX 
; 
;DEPENDENCIES	none 
; 
;EXAMPLE 
;		if not PriMapState('\\server\printer1') 
;		 "printer1 not connected!" 
;	        endif 
; 
;CODE 
function PriMapState($_Pri)
if @inwin=1
 if len(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_Pri))
  if split(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),",")[0]=$_Pri
   $PriMapState=2
  else
   $PriMapState=1
  endif
 endif
else
 dim $_Root,$_C,$_C2 $_Root="HKLM\System\CurrentControlSet\control\Print\Printers"
 for $_C=0 to 259
  $_C2=enumkey($_Root,$_C)
  If instr(READVALUE($_Root+"\"+$_C2,"Port"),$_Pri)
   If instr(READPROFILESTRING("%windir%\win.ini","windows","device"),$_Pri)
    $PriMapState = 2
   Else
    $PriMapState = 1
   Endif
  Endif
  if $_C2=259 $_C=$_C2 endif
 next
endif
endfunction


nige1979
(Fresh Scripter)
2009-05-14 02:18 PM
Re: Changing the Mapped Printers Servername

Ok, thanks for your help. I have done this and still get the same error.... anymore ideas people.....

Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 02:29 PM
Re: Changing the Mapped Printers Servername

Change
 Code:
	$PtrMapState = $PriMapState("\\" + $OldServer + "\" + $Ptr)
to
 Code:
	$PtrMapState = PriMapState("\\" + $OldServer + "\" + $Ptr)
, include the PriMapState UDF and you should be good..

Glenn


nige1979
(Fresh Scripter)
2009-05-14 02:40 PM
Re: Changing the Mapped Printers Servername

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.....

Anymore helpful ideas.....


Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 03:06 PM
Re: Changing the Mapped Printers Servername

Here's what I used recently:


;; KixGenerated: 2009/05/14 12:08:57 
; 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 $Printer						; printer BaseName 
Dim $OldPSvr, $NewPSvr					; 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 = '\\oldprintserver'
$NewPSvr = '\\newprintserver'
;############################################################## 
 
 
; 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')
 
 
; 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
 
  If $I = 0
    Cls
    ; Display the default printer 
    If $DefPtr
      'Default printer is ' Split($DefPtr, ',')[0] ? ?
    Else
      'Default printer is not defined.' ? ?
    EndIf						; process until an EOD error 
  EndIf
 
  '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
    $I = 0						; restart the enumeration 
  Else
    ' - OK' ?
    $I = $I + 1						; increase the enumeration pointer 
  EndIf
  $Value = EnumValue($LPKey, $I)
Loop
 
 

Just change the "oldprintserver" and "newprintserver" names.

Glenn

nige1979
(Fresh Scripter)
2009-05-14 03:14 PM
Re: Changing the Mapped Printers Servername

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
 
 


Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 03:19 PM
Re: Changing the Mapped Printers Servername

Dang - change
 Code:
Dim $OldPServer, $NewPServer				; old/new print server names 
to
 Code:
Dim $OldPSvr, $NewPSvr				; old/new print server names 
The var name defined doesn't match the one used. I must have grabbed the one from my dev folder. I'll fix my original post.

Glenn


nige1979
(Fresh Scripter)
2009-05-14 03:29 PM
Re: Changing the Mapped Printers Servername

Ok, now that goes past that point now but errors on line 65
 Code:
 $DefPtr = ReadValue('HKEY_CURRENT_USER\Software\Microsoft\Windows 


How do i pause the error on the screen so i can read it before it disappears....


Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 03:35 PM
Re: Changing the Mapped Printers Servername

Run the script from a command line.

nige1979
(Fresh Scripter)
2009-05-14 03:42 PM
Re: Changing the Mapped Printers Servername

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 ???

nige1979
(Fresh Scripter)
2009-05-14 03:44 PM
Re: Changing the Mapped Printers Servername

The default printer is connected locally, just thought i would mention this !?!?!?!

Gargoyle
(MM club member)
2009-05-14 03:55 PM
Re: Changing the Mapped Printers Servername

The error is stating that you have not defined [dimmed] the variable $Printer.

 Code:
Dim $Printer


nige1979
(Fresh Scripter)
2009-05-14 03:57 PM
Re: Changing the Mapped Printers Servername

??? ok what does that mean, do i need to enter something somewhere ??? sorry to be such a useless noob....

BradV
(Seasoned Scripter)
2009-05-14 04:17 PM
Re: Changing the Mapped Printers Servername

Just where the other "Dim" statements are in the script, add the one Gargoyle gave you.

nige1979
(Fresh Scripter)
2009-05-14 04:55 PM
Re: Changing the Mapped Printers Servername

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.....

nige1979
(Fresh Scripter)
2009-05-14 05:05 PM
Re: Changing the Mapped Printers Servername

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....

Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 05:08 PM
Re: Changing the Mapped Printers Servername

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


nige1979
(Fresh Scripter)
2009-05-14 05:17 PM
Re: Changing the Mapped Printers Servername

Ok it doesnt even do it in batches of 4 !!! i just ran it on a pc that had 4 mapped printers.... the 1st time it changed 2 printers the 2nd time it changed 1 and the 3rd time it changed the last printer.....

Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 06:01 PM
Re: Changing the Mapped Printers Servername

OK - I added some printers from our old print server and ran the code. It seems that the index of printers being enumerated from the registry is changing. I updated the code to restart the enumeration each time a change is made and it has successfully moved all 3 printers (of the 7 total) in one pass.

My original post is updated.

Glenn


nige1979
(Fresh Scripter)
2009-05-14 06:52 PM
Re: Changing the Mapped Printers Servername

Glenn and co, I cannot thank you enough for your help. Works a dream now...

Great site and great support and help from the Mods... Keep up the good work....


Glenn BarnasAdministrator
(KiX Supporter)
2009-05-14 07:15 PM
Re: Changing the Mapped Printers Servername

Great - thanks for the follow-up!

Glenn