Page 1 of 1 1
Topic Options
#199967 - 2010-09-20 03:42 PM New Print Server and Login Script
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
Hello all,

I've been looking through the posts in relation to switching print servers at our office and I have a very basic question or two. I'm just looking for a simple approach to move users from print queues on one print server to print queues on another (new) print server. I was planning on using the same print share names.

I just wanted to add some code to my current login script and make the change on users computers as they log into the network. The office computes are about 99% XP and 1% Windows 7.

Here's one of the codes that was suggested by someone that looked pretty straight forward:

 Code:
$NewServer="\\new\"
$OldServer="\\old\"
$Cnt=0
$SubKey= "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Printerports"
$PrintName=EnumValue ($SubKey, $Cnt)
While $PrintName <> ""
       If Left($PrintName, Len($OldServer))=$OldServer
               $NewPrintName=$NewServer + SubStr($PrintName,Len($OldServer) + 1,Len($PrintName))
        ? "Deleting; " $PrintName "..."
        $= DelPrinterConnection("$PrintName")
        ? @SERROR
        ? "Connecting; " $NewPrintName "..."
        $= AddPrinterConnection("$NewPrintName")
        ? @SERROR
Else
        $Cnt= $Cnt + 1
Endif
$PrintName=EnumValue ($Subkey, $Cnt)

Loop



Now to show how little I know....if I added this code to my current login script (and normally I would add it to the end of my login script), would it run the whole login script multiple times because there is a "loop" call in it? If that is the case, I assume I could get around it by putting it in the beginning of my login script?

There are of course plenty of other printer queue migration scripts on this board....again, I just choose this one since it was recommended by someone.

Any help, comments, or suggestions are very welcomed.

Thank you for your time.

Top
#199968 - 2010-09-20 03:57 PM Re: New Print Server and Login Script [Re: rbbigdog]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Welcometo KORG!

The "loop" marks the end of the loop begun by the "While". Doesn't matter where you put this. The code loops because it enumerates each of the registry keys looking for the one to delete.

This may work, but enumerating registry keys while you are manipulating them isn't usually a good idea. This code looks at a key, determines if it should be deleted, and then either deletes the key (and adds a new printer) or moves on to the next one. Adding the printer adds a value to the key - the same one you're enumerating.

It would be more stable to enumerate the list of printer keys and build an array. Then, enumerate the array, identify the printer(s) to change and make the changes. The new printers are not added to the array when they are added to the registry, reducing the effort and preventing any confusion.

I believe the "load reg to array / enum array" method of printer migration has been discussed here before.

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

Top
#199969 - 2010-09-20 04:09 PM Re: New Print Server and Login Script [Re: Glenn Barnas]
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
Thanks for the quick feedback Glenn and the Welcome!

Yes, I remember reading some of the posts about using an "array" for printer migration. I'll take another look at those scripts and see if they make sense to me \:\) I hope everyone doesn't mind if I post another question or two.

Again, I am trying to go through these posts (and learning what I can) before asking questions.

Thank you,

Top
#199972 - 2010-09-20 05:50 PM Re: New Print Server and Login Script [Re: rbbigdog]
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
So, here's part of some other code that I was also looking at that does use an array:

 Code:

$OldPrinters = '\\OLD01\Printer1',
'\\OLD01\Printer2',
'\\OLD01\Printer3',
'\\OLD01\Printer4',
'\\OLD01\Printer5',
'\\OLD01\Printer6'

$Printers = '\\NEW01\Printer1',
'\\NEW01\Printer2',
'\\NEW01\Printer3',
'\\NEW01\Printer4',
'\\NEW01\Printer5',
'\\NEW01\Printer6'

If @wksta <> 'OLD01'

  ; Delete Old Printer(s)
  For Each $Printer in $OldPrinters
    If PriMapState($Printer) <> ''
      $Rc = DelPrinterConnection($Printer)
    EndIf
  Next

  ; Add New Printer(s)
  For Each $Printer in $Printers
    If PriMapState($Printer) = ''
      $Rc = AddPrinterConnection($Printer)
    EndIf
  Next
EndIf



Will this code only delete/add printers that are currently installed or would it add all printers listed in the $printers array?

Also, I'm not sure I fully understand the " If @wksta <> 'OLD01' " part of the script or maybe why it would be needed. I understand that the script will only run if the name of the system isn't 'OLD01', but why is that required? Is it because the script could delete the printers from the server? If that is true, than wouldn't you want to add 'NEW01' to this IF statement too?

Sorry for all the questions.

Thank you,

Top
#199974 - 2010-09-20 07:29 PM Re: New Print Server and Login Script [Re: rbbigdog]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
No problem with questions - it's how we learn and share.

Without actually providing code, let's think about the process a moment.

You have 6 printers in the environment. 6 old printers and 6 new printers.

Not every user has every printer, so - you need to determine which printers are currently connected to the old server and disconnect them & then connect to the new.

Try this logic:
 Code:
$aPrinters = 'name1', 'name2', (and so on)
$OldSvr = '\\oldps\'
$NewSvr = '\\newps\'

For Each $Printer in $aPrinters  ; enumerate list of all printer names
  If PriMapState($OldSvr + $Printer) ; is it mapped to old server?
    DelPrinterConnection($OldSvr + $Printer)  ; disconnect from old
    AddPrinterConnection($NewSvr + $Printer)  ; connect to new
  EndIf
Next  ; check next printer
This basically checks every possible printer that was moved from old to new server. If the user is connected to the old server\printer, disconnect it and then connect to the new print server. If they aren't connected to a specific printer, ignore it.

I'd enhance this just a tad by identifying the old default printer and pointing it to the new share after all printers are mapped.

The code example above is untested and illustrates a concept that you will need to polish and refine.. it should be a good nudge in the right direction. Don't forget to include the PriMapState UDF in your code.

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

Top
#199975 - 2010-09-20 07:32 PM Re: New Print Server and Login Script [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Ah - and to answer the other question.. the If @WKSTA <> 'Old' is there to prevent the printers on the old print server from being deleted if the script is run there. The "old" printers would not exist (in theory) on the new print server, so no extra logic is needed.

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

Top
#199987 - 2010-09-21 03:02 PM Re: New Print Server and Login Script [Re: Glenn Barnas]
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
@Glenn....Thank you for the code and explanantions.

I've seen UDF listed many times before, so I've done some reading on that. I also saw a few code examples of setting the default printer, including using a UDF function.

I would think you would need to re-arrange the code some to Add Printer and Set Default Printer before Removing Printer. Even though I don't think this is correct (and I haven't tested this yet), I've come up with this:

 Code:

$aPrinters = 'Print1', 'Print2', 'Print3', 'Print4', 'Print5', 'Print6'
$OldSvr = '\\OLDPS\'
$NewSvr = '\\NEWPS\'

For Each $Printer in $aPrinters  ; enumerate list of all printer names
  If PriMapState($OldSvr + $Printer) ; is it mapped to old server?
    AddPrinterConnection($NewSvr + $Printer)  ; connect to new server
 	if not @error
	  if Primapstate($Printer) = 2
	    $rc=SetDefaultPrinter($Printer)  ; set default printer
	  endif
	    DelPrinterConnection($OldSvr + $Printer)  ; disconnect from old
	EndIf	
  EndIf
Next  ; check next printer

; PriMapState UDF Function
function PriMapState($_Pri)
	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
endfunction



It seems wrong since I wouldn't think I would call PriMapState twice.

Thank you,

Top
#199988 - 2010-09-21 03:06 PM Re: New Print Server and Login Script [Re: rbbigdog]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
See the following FAQ, I think it will answer your question about it "running twice".

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943

Top
#200143 - 2010-10-03 07:34 PM Re: New Print Server and Login Script [Re: Allen]
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
I finally got my new print server online and could do some testing of the below script. It removes and adds the printers without issues, but sometimes has issues with the default printer. It might set the default printer correctly, but sometimes it doesn't set a default at all or selects a different printer as the default (it almost seems random).

Just to recap, all printer names are the same on both the old and new print servers (just a new server name).

Any suggestions would be appreciated.


 Code:

$oldPrinters = 'Print1', 'Print2', 'Print3', 'Print4', 'Print5', 'Print6'
$defaultprinter = GetDefaultPrinter()
$OldSvr = '\\OldServer\'
$NewSvr = '\\NewServer\'

For Each $Printer in $oldPrinters  ; enumerate list of all printer names
  If PriMapState($OldSvr + $Printer) ; is it mapped to old server?
    AddPrinterConnection($NewSvr + $Printer)  ; connect to new server
 	if not @error
	    DelPrinterConnection($OldSvr + $Printer)  ; disconnect from old
	EndIf	
  EndIf
	If $defaultprinter = ($OldSvr + $Printer)  ; select old default printer	
		$rc = SetDefaultPrinter($NewSvr + $Printer)
	EndIf
Next  ; check next printer

; GetDefaultPrinter UDF Function
function GetDefaultPrinter()
    $GetDefaultPrinter = join(split(readvalue("HKEY_USERS\"+@sid+"\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'')
endfunction

; PriMapState UDF Function
function PriMapState($_Pri)
	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
endfunction



Thanks!

- RBB

Top
#200144 - 2010-10-03 08:10 PM Re: New Print Server and Login Script [Re: rbbigdog]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
LOL
Had the same issue some time ago. Sometimes the correct printer is set as default and sometimes another printer was set as default. Seemed to be totally random.

it turns out to be best practice to add all needed printers first and set the default printer when all needed printer are added. So adding all printers that are needed and setting one as default when all printers are there should fix your issue. Never had any issues with this in NT4, 2000 and XP. Windows 7 seems to be pickier in this.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200145 - 2010-10-03 08:34 PM Re: New Print Server and Login Script [Re: Mart]
rbbigdog Offline
Fresh Scripter

Registered: 2007-11-19
Posts: 6
Good....I have to admit that I always feel a little better when someone else had seen the same problem before \:\/

I was working on the problem just a little bit ago (before seeing your reply) and I changed the script location a bit that seemed to work, but it wasn't at the end, but within the PriMapState IF statement (It doesn't sound like that is a good practice though).

 Code:
For Each $Printer in $oldPrinters  ; enumerate list of all printer names
  If PriMapState($OldSvr + $Printer) ; is it mapped to old server?
    AddPrinterConnection($NewSvr + $Printer)  ; connect to new server
 	if not @error
	    DelPrinterConnection($OldSvr + $Printer)  ; disconnect from old
	EndIf	
	If $defaultprinter = ($OldSvr + $Printer)  ; select old default printer	
		$rc = SetDefaultPrinter($NewSvr + $Printer)
	EndIf
  EndIf

Next  ; check next printer


But I assume you meant after all new printers are added, so after NEXT (I'll try that)

 Code:
For Each $Printer in $oldPrinters  ; enumerate list of all printer names
  If PriMapState($OldSvr + $Printer) ; is it mapped to old server?
    AddPrinterConnection($NewSvr + $Printer)  ; connect to new server
 	if not @error
	    DelPrinterConnection($OldSvr + $Printer)  ; disconnect from old
	EndIf	
  EndIf

Next  ; check next printer

	If $defaultprinter = ($OldSvr + $Printer)  ; select old default printer	
		$rc = SetDefaultPrinter($NewSvr + $Printer)
	EndIf



I'm 98% XP with a few Win7 systems (but adding more soon).

-RBB


Edited by rbbigdog (2010-10-03 08:42 PM)

Top
#200146 - 2010-10-03 10:27 PM Re: New Print Server and Login Script [Re: rbbigdog]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: rbbigdog

...

But I assume you meant after all new printers are added, so after NEXT (I'll try that)

...


Yes sir. That is what I meant.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#200149 - 2010-10-04 01:41 PM Re: New Print Server and Login Script [Re: rbbigdog]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
 Originally Posted By: gbarnas
I'd enhance this just a tad by identifying the old default printer and pointing it to the new share after all printers are mapped.
See - told ya to set the default printer as the last step! \:D
(But do you listen? NO! We just don't communicate anymore...) \:D

Glad to hear you (almost) got it working!

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

Top
#200150 - 2010-10-04 01:45 PM Re: New Print Server and Login Script [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Also...

To further preserve your sanity, collect the data first, then process.
Load the default printer setting into a var, then scan and change the printers, then check the default printer variable and update the setting.

Windows will automatically set a default printer when the current default is deleted. It's possible that after you change all the printers that the default printer will be pointing to a different device. Capturing it first and checking the captured value will insure that you set the correct device as the default.

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

Top
Page 1 of 1 1


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

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

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

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