Page 1 of 1 1
Topic Options
#192464 - 2009-02-24 10:00 AM Deleting and re-installing network printers
Ronnie Offline
Fresh Scripter

Registered: 2009-02-23
Posts: 5
Loc: Denmark
Hi all,

I want to change the naming standard for all printers in my company. This goes for both the sharenames and printer names. The idea is that I'll do this outside normal working hours and then for a period of time update the login script so that it first checks if a network printer is installed, and if this is the case it deletes the network printer and then re-installs it with the new name. This will be done for each network printer in the company, and if the printer isn't installed on the specific pc the script will just move on to check for the next printer. However I can't get this to work properly.

I'm not sure the above can be accomplished using a batch login script, so if anybody can help me scripting this using Kix I'd be very thankful.

This is an example of the script for a printer with old printer name OKI C5600 and sharename OKIC5600 and new printer name Herlev - Stuen - ANBL - Oki C5600 and sharename ANBLOkiC5600:

if not exist \\fs\OKIC5600 goto ok_OKIC5600
rundll32 printui.dll,PrintUIEntry /dn /n\\fs\OKIC5600 /j"OKI C5600"
rundll32 printui.dll,PrintUIEntry /in /n\\fs\ANBLOkiC5600 /j"Herlev - Stuen - ANBL - Oki C5600"
:ok_OKIC5600

Can anybody tell me why this isn't working? I think the problem is that "if not exist \\fs\OKIC5600 goto ok_OKIC5600" looks for a printer with the sharename OKIC5600 on the print server named fs, and this no longer exists since I've changed the sharename to ANBLOkiC5600, and because this no longer exists on the print server it goes to ":ok_OKIC5600".

So the challenge is to change the \\fs\OKIC5600 part to instead search for the network printer on the local pc, but how can I do this with Kix so it works for all pc's in the domain?

Thanks in advance,
Ronnie

Top
#192466 - 2009-02-24 10:31 AM Re: Deleting and re-installing network printers [Re: Ronnie]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you should search this forum as it's full of similar examples.

good keyword would be primapstate
_________________________
!

download KiXnet

Top
#192474 - 2009-02-24 03:33 PM Re: Deleting and re-installing network printers [Re: Lonkero]
Ronnie Offline
Fresh Scripter

Registered: 2009-02-23
Posts: 5
Loc: Denmark
Thanks for the advice. I've been looking around and this seems to be closest post to what I want to achieve http://www.kixtart.org/forums/ubbthreads...rue#Post187369. But it's for changing print server and not printer names, so I hope someone can help me modifying the code?

Thanks in advance,
Ronnie

Top
#192479 - 2009-02-24 04:35 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you are not going to change the printer names in the script, right?

you want to connect to new sharename/printername after the rename has occured?

if this is not correct, then it's a whole new deal. but if this is what you want, then the procedure is same as when you are changing printservers.
_________________________
!

download KiXnet

Top
#192489 - 2009-02-24 05:55 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Have a look at the link below. There is some working code in there that (almost) does what you need.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=1&Number=191944
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#192560 - 2009-02-26 01:29 PM Re: Deleting and re-installing network printers [Re: Mart]
Ronnie Offline
Fresh Scripter

Registered: 2009-02-23
Posts: 5
Loc: Denmark
Hi and thanks for your help so far. However I'm quite new with this stuff and I really don't know how to modify the code to make it match my requirements. As far as I can see the code is for changing printserver, which isn't what I'm doing. I'm hoping tha someone can come up with a code that can help me here?

Thanks in advance,
Ronnie

Top
#192562 - 2009-02-26 01:33 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I'm still waiting for your answer to my clarification question.

as I said above, if I understood correctly, the given instructions are exactly what you want.
if we have misunderstood your needs, please explain further, what you really want the script to do.
_________________________
!

download KiXnet

Top
#192563 - 2009-02-26 01:49 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
First please answer any question that are asked because this will help us help you. Below is the code from the link I posted earlier but then with comments. See if you can understand any of it please ask if you have questions.

 Code:
Break on

;Gets all currently installed printers.
$printers = EnumPrinterConnections2()

;Get the current default printer.
$defaultprinter = GetDefaultPrinter()

;Put the name of the old server in a variable.
$oldserver = "oldservername"

;Put the name of the new server in a variable.
$newserver = "newservername"

;This For - Each loop does the actions between the For and Next
;lines for all installed printers gathered above.
For Each $printer in $printers
	;If the name of the old server is found in the name of the currently
	;installed printer do the actions below.
	If InStr($printer, $oldserver)
		;Delete the old printer.
		$rc = DelPrinterConnection($printer)
		;Split the old printer name on the name of the old server.
		$newprinter = Split($printer, $oldserver)
		;Join the old printer name with the name of the new server
		;and put the outcome in the $newprinter variable.
		$newprinter = Join( $newprinter, $newserver ")
		;Install the new printer.
		$rc = AddPrinterConnection($newprinter)
		;If the old printer deleted above was the default printer
		;then set the new printer as default.
		;If not then skip this part and move on the next printer.
		If $printer = $defaultprinter
			$rc = SetDefaultPrinter($newprinter)
		EndIf
	EndIf
Next


Edited by Mart (2009-02-26 01:50 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#192565 - 2009-02-26 02:14 PM Re: Deleting and re-installing network printers [Re: Mart]
Ronnie Offline
Fresh Scripter

Registered: 2009-02-23
Posts: 5
Loc: Denmark
Hi and sorry about that. Somehow I missed your question.

You're right that I don't want to change the printer names in the script. I'll be doing this directly on the print server prior to updating the login script with the Kix code.

Basically what I want is for the script to check the pc for any network printers installed with the old names, and if it detects any it should delete the printer and install the same printer with the new name. However only previously installed printers should be re-installed so just deleting all printers and then instaling all avilable network printers will not work. Keeping the default printer would also be a good thing.

Most of your code makes sense with the comments you've put in, but what is confusing me is that you specify a old and a new server in your code while my print server is named FS and this will not change only printer names and share names will.

Thanks again,
Ronnie


Edited by Ronnie (2009-02-26 02:53 PM)

Top
#192569 - 2009-02-26 02:50 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
You will have to create some sort of map file.

I.E.

OldPrinter = NewPrinter
OldPrinter2 = NewPrinter2
etc = etc

Once you have that, you can Enum the printers that are currently installed and retrieve the default while you are at it.

Now that you have all of the information about the old printers, you can delete them.

And finally you look at your map and see which printer is which and install the new one.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#192570 - 2009-02-26 02:51 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, the primapstate() is your solution.
with it you can see if a printer is connected and if it is the default printer.

with this information, you can map the new connection with addprinterconnection(), etc.
_________________________
!

download KiXnet

Top
#192632 - 2009-02-27 08:36 AM Re: Deleting and re-installing network printers [Re: Gargoyle]
Ronnie Offline
Fresh Scripter

Registered: 2009-02-23
Posts: 5
Loc: Denmark
Hi Gargoyle,

Ok - that makes sense, but how can I call the map file from the Kix code?

Regards,
Ronnie


Edited by Ronnie (2009-02-27 11:14 AM)

Top
#192638 - 2009-02-27 12:54 PM Re: Deleting and re-installing network printers [Re: Ronnie]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
There are several different options but the easiest to work with would most likely be to set it up as INI type file and use the READPROFILESTRING()
1. Set it up like an INI
[Section]
old=new
old2=new2

 Code:
For each $Printer in $PrinterArray
  $NewPrinter = ReadProfileString(@ScriptDir+"\Printers.INI","Section",$Printer)
  DelPrinterConnection($Printer)
  AddPrinterConnection($NewPrinter)
Next


Obviously this is only an example and will take some flushing out but should give you the idea of how to start.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#192641 - 2009-02-27 01:20 PM Re: Deleting and re-installing network printers [Re: Gargoyle]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
to store only already renamed printers, I would suggest this type of ini:
 Code:
[OldPrinterShare]
newShare=\\serrver\share
newName=\\server\printername


then you could go with this kind of code:
 Code:
For each $Printer in split(left(ReadProfileString(@ScriptDir+"\Printers.INI","",""),-1),chr(10))
  if primapstate($Printer)
   AddPrinterConnection(ReadProfileString(@ScriptDir+"\Printers.INI",$Printer,"NewShare"))
  endif
Next
_________________________
!

download KiXnet

Top
#192740 - 2009-03-06 09:30 PM Re: Deleting and re-installing network printers [Re: Lonkero]
ffangus Offline
Just in Town

Registered: 2009-02-09
Posts: 1
Loc: United States
I am looking for something similar, but going to the next level. I am attempting to move off an old print server and move it to a new print server with a new name. The new server has the same IP's but a different share and printer name.
Top
#192741 - 2009-03-08 12:19 AM Re: Deleting and re-installing network printers [Re: ffangus]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
if you bother reading the thread, you will find out that, what you are after is exactly the same thing.
_________________________
!

download KiXnet

Top
#194341 - 2009-06-22 09:35 PM Re: Deleting and re-installing network printers [Re: Lonkero]
dataspike Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
I ran in to this same issue/question, so I thought I'd share my results.

These functions allow me to enumerate the printers installed, and change the printer name (ONLY).

ChangePrinterName('OldName','NewName')

 Code:
Function ChangePrinterName($OldPrinterName, $NewPrinterName)
	$printers = arrenumkey('HKCU\Printers\Connections')
	For Each $printer in $printers
		If InStr($printer,$OldPrinterName)
			$printer = Split($printer,',')
			$old = '\\' + $printer[2] + '\' + $printer[3]
			$new = '\\' + $printer[2] + '\' + $NewPrinterName
 			$rc = AddPrinterConnection($new) 
			If Not @error
				If Primapstate($old) = 2
					$rc = SetDefaultPrinter($new)
				EndIf
				$rc = DelPrinterConnection($old)
		 	EndIf
		EndIf
	Next
EndFunction

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

Function arrenumkey($regkey)
	Dim $Keylist, $c
	If Not KeyExist($regkey)	
		Exit 87		
	EndIf
	Do
		$Key = $Key+'|'+EnumKey($regkey,$c)
		$c = $c + 1
	Until @error
	$arrenumkey = Split(SubStr($Key,2,Len($Key)-2),'|')
EndFunction

I am sure it could be configured very easily to change server name as well.

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 323 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.071 seconds in which 0.027 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