Page 1 of 2 12>
Topic Options
#193841 - 2009-05-14 01:30 PM Changing the Mapped Printers Servername
nige1979 Offline
Fresh Scripter

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-14 01:39 PM)
Edit Reason: Added Code Tags

Top
#193842 - 2009-05-14 01:39 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
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.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#193843 - 2009-05-14 02:00 PM Re: Changing the Mapped Printers Servername [Re: Benny69]
nige1979 Offline
Fresh Scripter

Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Which bit do i copy and where do i paste it on the script. Sorry not very good with this stuff.....
Top
#193844 - 2009-05-14 02:06 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
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
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#193845 - 2009-05-14 02:18 PM Re: Changing the Mapped Printers Servername [Re: Benny69]
nige1979 Offline
Fresh Scripter

Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
Ok, thanks for your help. I have done this and still get the same error.... anymore ideas people.....
Top
#193846 - 2009-05-14 02:29 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Change
 Code:
	$PtrMapState = $PriMapState("\\" + $OldServer + "\" + $Ptr)
to
 Code:
	$PtrMapState = PriMapState("\\" + $OldServer + "\" + $Ptr)
, include the PriMapState UDF and you should be good..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193847 - 2009-05-14 02:40 PM Re: Changing the Mapped Printers Servername [Re: Glenn Barnas]
nige1979 Offline
Fresh Scripter

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

Anymore helpful ideas.....

Top
#193851 - 2009-05-14 03:06 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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

Edited by Glenn Barnas (2009-05-14 06:09 PM)
Edit Reason: Updated code

_________________________
Actually I am a Rocket Scientist! \:D

Top
#193855 - 2009-05-14 03:14 PM Re: Changing the Mapped Printers Servername [Re: Glenn Barnas]
nige1979 Offline
Fresh Scripter

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
 
 

Top
#193856 - 2009-05-14 03:19 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193859 - 2009-05-14 03:29 PM Re: Changing the Mapped Printers Servername [Re: Glenn Barnas]
nige1979 Offline
Fresh Scripter

Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
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....

Top
#193861 - 2009-05-14 03:35 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Run the script from a command line.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193862 - 2009-05-14 03:42 PM Re: Changing the Mapped Printers Servername [Re: Glenn Barnas]
nige1979 Offline
Fresh Scripter

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

Attachments
180.jpg
Description:



Top
#193863 - 2009-05-14 03:44 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
nige1979 Offline
Fresh Scripter

Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
The default printer is connected locally, just thought i would mention this !?!?!?!
Top
#193864 - 2009-05-14 03:55 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
The error is stating that you have not defined [dimmed] the variable $Printer.

 Code:
Dim $Printer
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193865 - 2009-05-14 03:57 PM Re: Changing the Mapped Printers Servername [Re: Gargoyle]
nige1979 Offline
Fresh Scripter

Registered: 2009-05-14
Posts: 24
Loc: United Kingdom
??? ok what does that mean, do i need to enter something somewhere ??? sorry to be such a useless noob....
Top
#193868 - 2009-05-14 04:17 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Just where the other "Dim" statements are in the script, add the one Gargoyle gave you.
Top
#193872 - 2009-05-14 04:55 PM Re: Changing the Mapped Printers Servername [Re: BradV]
nige1979 Offline
Fresh Scripter

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

Edited by nige1979 (2009-05-14 05:04 PM)

Top
#193874 - 2009-05-14 05:05 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
nige1979 Offline
Fresh Scripter

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....
Top
#193875 - 2009-05-14 05:08 PM Re: Changing the Mapped Printers Servername [Re: nige1979]
Glenn Barnas Administrator Offline
KiX Supporter
*****

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! \:D

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.076 seconds in which 0.025 seconds were spent on a total of 15 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org