Page 1 of 1 1
Topic Options
#213860 - 2020-07-02 09:48 PM Hoping for easy fix... Printing related
Onsitejim Offline
Just in Town

Registered: 2020-07-02
Posts: 4
Loc: Canada
I'm utilizing a script I found by Bob Kelly to take existing network mapped printers, delete them & then re-create them pointing via the new server as I'm trying to decommission the old server within the next few days.

Although the script is working beautifully, I've got to deploy this into our main Kix script which will deploy to over a 100 users, & I need to have the script first look up & see what the current default printer is, & then when all is said & done & the new printers are in place I need whatever the new version of the old printer is to be set automatically as default.

I've migrated the printers from old server to new using export/import in print management, so literally the only thing that is changing is the server name.

The script I'm using which needs to be modified a to add in the defaults is as follows:

 Code:
$NewServer="\\NEW-SERVER\"
$OldServer="\\OLD-SERVER\"
$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


END OF CODE:

Like I said, the code works awesome but when it runs it kind of randomly decides on a printer to set as default, & considering that out of the 100+ workstations there are 150+ users that all point in random directions to their default printers that's why I want to fix this using KIX as we are using it for login scripting anyway & if it's just a matter of adding a few extra lines of code that would sure make my life a lot easier.

Thoughts?

Thanks in advance everyone.


Edited by Mart (2020-07-03 09:21 AM)
Edit Reason: Please use code tags when posting code.

Top
#213861 - 2020-07-03 02:24 PM Re: Hoping for easy fix... Printing related [Re: Onsitejim]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I have a login script that can unmap/remap printers and set the default printer via a centrally-managed lookup table (computer=printer) or via a local environment variable. The environment variable can reference a network OR local printer. The printer mapping logic also allows defining what I refer to as "Default-Eligible" status. You can have a primary printer that's default (by default) and the user's default printer can change as additional printers are mapped, right down to the definitions noted above.

This is a commercial product, written in Kix, and tokenized (no mods), but you can extend it with code you write that's dynamically loaded and executed, including access to internal vars for key information. Fully documented in a 71-page user manual, it's free for use in networks with 1-2 domain controllers, and licensed by DC in larger environments.

That said - you could identify and record the current default printer NAME somewhere, then after changing the printers, match the saved name to the current printer share and set that as default. I've deployed my script in many environments where I had to deal with infrastructure upgrades and migrations with relative ease. PM me if you want to check out the user guide. I am just putting my website back online with my tools and UDF library after a nearly 4-year hiatus.

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

Top
#213863 - 2020-07-07 12:45 AM Re: Hoping for easy fix... Printing related [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Here is your code, with the GetDefaultPrinter() UDF added and then its value used to remap the default after printers are added. I'm not able to test it, but I don't see why it wouldn't work as long as the printer name on the new server is the same as it was on the old server. Give it a test.

 Code:
$NewServer = "\\NEW-SERVER\"
$OldServer = "\\OLD-SERVER\"
$defPrinterName = GetDefaultPrinter()
$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

$ = SetDefaultPrinter($NewServer + "\" + $defPrinterName)

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


The UDF can be found here...
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=212112

Top
#213865 - 2020-07-07 01:54 PM Re: Hoping for easy fix... Printing related [Re: Glenn Barnas]
Onsitejim Offline
Just in Town

Registered: 2020-07-02
Posts: 4
Loc: Canada
Thank you for replying Glenn, I would be interested & the environment in this case has a PDC & a BDC but then a couple of specialized servers (SQL, etc...)

The specifics of this client is that I'm decommissioning a server that used to be a print server, & I've already migrated the print drivers/queues to the new print server but I honestly don't want to run around to over 100 PC's to do this manually if I can script it instead & besides using Kixtart to script for drive mappings I honestly don't have a lot of experience with it so any particular help would be greatly appreciated.

Top
#213866 - 2020-07-07 02:19 PM (NA) Re: Hoping for easy fix... Printing related [Re: Onsitejim]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Jim - www.barnas.us/software is the URL to follow for the ULS download and User Guide. 2 DCs doesn't require a license regardless of how many other computers are on the network.

In the configuration options is a feature that unmaps all printers, allowing you to re-map them. You can also build a table of default printer by computer. Using the getDefaultPrinter() function in your current script and writing a log of computer=defprinter values via WriteProfileString will build that table for you.

Drive mapping works similarly - you can unmap everything, or everything except certain letters, then map drives and shares (no drive letter). Most resource mappings can be controlled by user group, computer group, OU, computer OU, AD Site, IP Address range, and even AD Attributes (like "manager" or "department")

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

Top
#213867 - 2020-07-07 02:24 PM (NA) Re: Hoping for easy fix... Printing related [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Another thought.. This will simplify your logic if the printer names are the same:
 Code:
$OldPrinter = '\\server\printer3'
$NewPrinter = Replace($OldPrinter, 'server', 'newserver')
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#213868 - 2020-07-16 03:46 PM Re: Hoping for easy fix... Printing related [Re: ShaneEP]
Onsitejim Offline
Just in Town

Registered: 2020-07-02
Posts: 4
Loc: Canada
Just because I'm a relative Kix newbie, I just want to clarify 100%.

This appears as though you've included the UDF into the script, so the call references that piece internally in the script.

Am I reading that right?

Top
#213869 - 2020-07-16 03:57 PM Re: Hoping for easy fix... Printing related [Re: ShaneEP]
Onsitejim Offline
Just in Town

Registered: 2020-07-02
Posts: 4
Loc: Canada
So I just put this in place in my test script, & it did follow the steps to replace the printers with the new server versions, however it didn't appear to set the default printer back in place unfortunately.

The other thing that I'm struggling with and honestly this likely isn't too bad of a struggle is that one of the printers that's used in the administrative area all of the individual users have a setting in their printing preferences that enter a job accounting code for printing, but as that's only maybe a dozen people I can enter this manually for them once it changes over.

My real concern before I roll this out company wide is that I need that default printer to kick in properly for the users otherwise it's going to be a really long couple of days for me.

Thank you I appreciate all the help.

Top
#213870 - 2020-07-16 07:35 PM Re: Hoping for easy fix... Printing related [Re: Onsitejim]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
The small example I provided simply updated the printer - if you know where the old printer is, and the names are the same, that logic updates server with newserver. You still need to save and then re-apply the original default printer.

Shane posted the GetDefaultPrinter function, basically reading a registry value and pulling out a specific part of the result. That logic should read and save the default printer, get the current printer, delete that, change the server name and connect to that printer, then reset the default. His logic is just a bit more complex than my Replace(data,old,new) method but should still work just fine. Make sure you include all of his logic, including the function logic.

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

Top
#213871 - 2020-07-17 03:23 PM Re: Hoping for easy fix... Printing related [Re: Glenn Barnas]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Remember that SetDefaultPrinter is a little odd as it uses the actual printer NAME and not the SHARE NAME. So even if they're shared as the same name, if the actual printer name is not the same it would not work. So verify this.

Also...I just noticed that I have an extra "\" in the code I posted, since I didn't see that you already included it in the NewServer variable. Try this code, with a couple extra output lines for troubleshooting, and the extra "\" removed.

 Code:
$NewServer = "\\NEW-SERVER\"
$OldServer = "\\OLD-SERVER\"
$defPrinterName = GetDefaultPrinter()
? "Default printer found as: " + $defPrinterName

$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

? "Setting default printer to: " + $NewServer + $defPrinterName
$ = SetDefaultPrinter($NewServer + $defPrinterName)

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

Top
#213872 - 2020-07-17 03:39 PM Re: Hoping for easy fix... Printing related [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
And yes, your understanding of how the GetDefaultPrinter function is working is correct. UDF is short of User Defined Function. You can define whatever you want in UDFs at the bottom, or top of you script, and the kix executable will recognize these when it loads the script.

In this case, since the the UDF is only being called once, we really could have just put that single line of code towards the top, and not bothered with a UDF. But that's kind of the beauty of UDFs, they are made to be universal and plug n play, callable from anywhere in the code regardless of what the rest of the script contains.

Top
#213873 - 2020-07-18 07:27 PM Re: Hoping for easy fix... Printing related [Re: ShaneEP]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I've used the PrinterConnection UDF for many things including what you are doing. The version posted will handle getting/setting defaults. It also always uses the share name of the printer, no matter what action you are performing. Of note, this is an older version that works fine, but I did come up with a newer version that I never posted. I'm willing to post it if you find the need.

PrinterConnection -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=203840#Post203840

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

The rest of the UDFs are here -
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1

Top
#213874 - 2020-07-24 10:06 PM Re: Hoping for easy fix... Printing related [Re: Allen]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Glenn moved over to a .US website? What happened to the .net site?
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
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.068 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