#199967 - 2010-09-20 03:42 PM
New Print Server and Login Script
|
rbbigdog
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:
$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
|
|
|
|
#199969 - 2010-09-20 04:09 PM
Re: New Print Server and Login Script
[Re: Glenn Barnas]
|
rbbigdog
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
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:
$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
|
|
|
|
#199987 - 2010-09-21 03:02 PM
Re: New Print Server and Login Script
[Re: Glenn Barnas]
|
rbbigdog
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:
$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
|
|
|
|
#200143 - 2010-10-03 07:34 PM
Re: New Print Server and Login Script
[Re: Allen]
|
rbbigdog
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.
$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
|
|
|
|
#200145 - 2010-10-03 08:34 PM
Re: New Print Server and Login Script
[Re: Mart]
|
rbbigdog
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).
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)
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 293 anonymous users online.
|
|
|