ghangis
(Just in Town)
2008-04-28 05:19 PM
migrating print server

under request from NTDOC, I am starting this thread to continue a conversation from another thread re: migrating shared printers from one server to another...

I've been trying to get the PRIMAPSTATE UDF to work with no success. if someone can help me understand how to incorporate a UDF into my existing kix script. what I need to do is as follows...

if a user is mapped to \\serverA\printer1 I need the script to map \\serverB\printer1 then delete \\serverA\printer1. the only change is with the path as the share name is the same.

P.S. my apologies for "hijacking" the thread I previously posted on...

..help


eriqjaffe
(Hey THIS is FUN)
2008-04-28 07:01 PM
Re: migrating print server

The easiest way is just to copy and paste the function's code into your script somewhere (I prefer the end of the script, but it technically doesn't matter). KiXtart parses the script when it is run, and anything inside Function...EndFunction blocks gets treated as if it's a built-in KiXtart function.

Mart
(KiX Supporter)
2008-04-28 08:16 PM
Re: migrating print server

A quick and dirty one. I'm sure that are other (and shorter) ways of doing this but the script below should work. There are lots of comments in the code so you can see what each line does.

 Code:
Break on

;Enumerate the current (old) printer connections.
$printers = ArrayEnumKey("HKEY_CURRENT_USER\Printers\Connections")

;Get the default printer.
;Why? See If - Endif statement below.
$defaultprinter = GetDefaultPrinter()

;Split the default printername on \.
;This makes an array you can reference to an element later.
$defaultprinter = Split($defaultprinter, "\")

;Get just the default printer name. Strip off the server name.
;Get the contents of the highest element in the array created above.
$defaultprintername = $defaultprinter[Ubound($defaultprinter)]

;Do stuff for each printer found.
For Each $printer in $printers
	;
	;Split the key name on ,.
	;This makes an array you can reference to an element later.
	$printersplit = Split($printer, ",")
	;
	;Get just the printer name. No server, no comma's.
	;Get the contents of the highest element in the array created above.
	$printername = $printersplit[Ubound($printersplit)]
	;
	;Delete the old printer.
	$rc = DelKey("HKEY_CURRENT_USER\Printers\Connections\" + $printer)
	;
	;Connect the same printer but with the new server name.
	$rc = AddPrinterConnection("\\newserver\" + $printername)
	;
	;If the printer being added is the same as the old default printer then
	;set the new printer as default.
	If $printername = $defaultprintername
		;Set the default printer.
		$rc = SetDefaultPrinter("\\newserver\" + $printername)
	EndIf
Next


;=-=-=-=--= DO NOT MODIFY ANYTHING BELOW THIS LINE =-=-=-=--=
;=-=-=-=--= BELOW ARE UDF'S. THEY COME READY MADE. =-=-=-=--=


;HKEY_CURRENT_USER\Printers\Connections\,,jupiter,RTD-PRN003

;NAME          ArrayEnumKey
;
;ACTION        Creates an array of names of the subkeys contained in a registry key or subkey
;
;AUTHOR        Jens Meyer (sealeopard@usa.net)
;
;VERSION       1.2 (added error codes)
;              1.1
;
;DATE CREATED  2001/12/05
;
;DATE MODIFIED 2003/05/17
;
;KIXTART       4.12+
;
;SYNTAX        ARRAYENUMKEY($subkey)
;
;PARAMETERS    SUBKEY
;              Required string containing the key or subkey for which the subkeys will be enumerated
;
;RETURNS       Array containing the subkeys
;
;REMARKS       none
;
;DEPENDENCIES  none
;
;EXAMPLE       $retcode=arrayenumkey('HKEY_USERS')
;
;KIXTART BBS   http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000064
;
Function arrayenumkey($regsubkey)
  Dim $retcode, $subkeycounter, $currentsubkey, $subkeyarray

  If NOT KeyExist($regsubkey)
    Exit 87
  EndIf

  $subkeycounter=0
  Do
    $currentsubkey=EnumKey($regsubkey,$subkeycounter)
    If NOT @ERROR
      ReDim preserve $subkeyarray[$subkeycounter]
      $subkeyarray[$subkeycounter]=$currentsubkey
      $subkeycounter=$subkeycounter+1
    EndIf
  Until @ERROR

  $arrayenumkey=$subkeyarray
  Exit 0
EndFunction

;FUNCTION         GetDefaultPrinter()
;
;AUTHOR           Jochen Polster (jochenDOTpolsterATgmxDOTnet)
;
;VERSION          1.0
;
;VERSION HISTORY  1.0 2004/04/28 Initial release
;
;ACTION           Retrieves the current default Printer
;
;SYNTAX           GetDefaultPrinter()
;
;PARAMETERS       none
;
;REMARKS          won't work with 9x OS
;
;RETURNS          The current Users default Printer
;
;DEPENDENCIES     None !
;
;EXAMPLES         $default = GetDefaultPrinter

Function GetDefaultPrinter()
    $GetDefaultPrinter = Join(Split(ReadValue("HKEY_USERS\"+@sid+"\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'')
EndFunction


NTDOCAdministrator
(KiX Master)
2008-04-29 03:08 AM
Re: migrating print server

Don't forget that printer NAMES and SHARE NAME should match if they don't then that can cause problems.

ghangis
(Just in Town)
2008-04-29 03:39 PM
Re: migrating print server

Mart, your script is outstanding!!!

Thank you all very much for you patience.