Page 1 of 1 1
Topic Options
#171357 - 2006-12-12 11:17 AM ,Adding/removing network printers
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
Hi
Top
#171358 - 2006-12-12 11:20 AM Re: ,Adding/removing network printers [Re: Dugster]
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
Sorry - very new to this!

I'm trying to write a script to add and remove networked printers.
I need to say something like.....

If user is connected to \\oldserver\oldprinter then add \\newserver\newprinter and delete \\oldserver\oldprinter

with possibility of setting it to default if old printer was default.

If anyone can give me some pointers as to how to script this I would be eternally grateful.
Thanks

Top
#171360 - 2006-12-12 12:55 PM Re: ,Adding/removing network printers [Re: Dugster]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think the PriMapState() UDF can help you.
I presume your old printer server should still be running
PriMapState("\\OldServer\Printer1")
will return
1 if printer connected
2 if printer is default
nothing if not connected
I presume you can create a script enumerating all of your printers on the old server and if 1 or 2 is returned, remove it, install the one from the new server and if return was 2, make it default.

Top
#171517 - 2006-12-14 05:27 PM Re: ,Adding/removing network printers [Re: Witto]
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
Thanks for that - the link looks like a good one.
Unfortunately I'm totally clueless here - how would you enumerate the printers?
Also I assume in the script you sent $_Pri is replaced by the printer share? As I say - completely clueless!
Really appreciate any advice..

Top
#171519 - 2006-12-14 06:34 PM Re: ,Adding/removing network printers [Re: Dugster]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I presume you know all your printers on your printer server? Just create your own array.
I presume the same printers have been created on the new server... I also presume that printer names and printer shares have the same name?
You should not replace the $_Pri. You keep the UDF as it is and add it p.e. at the end of your script.
Here is a script that I did not test!!!
Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $SO
$SO=SetOption("Explicit", "ON")
$SO=SetOption("NoMacrosInStrings", "ON")
$SO=SetOption("NoVarsInStrings", "ON")
$SO=SetOption("WrapAtEOL", "ON")

Dim $OldServer, $NewServer, $ArrPtrs, $Ptr, $PtrMapState, $NUL
$OldServer = "OldServer"
$NewServer = "NewServer"
$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs
	$PtrMapState = $PriMapState("\\" + $OldServer + "\" + $Ptr)
	If 0 < $PtrMapState			
		$NUL = DelPrinterConnection($Ptr)
		If NOT @ERROR
			$NUL = AddPrinterConnection("\\" + $NewServer + "\" $Ptr)
			If 2 = $PtrMapState
				$NUL = SetDefaultPrinter("\\" + $NewServer + "\" $Ptr)
			EndIf
		Else
			? "Error deleting Printer Connection " + $Ptr
			? "Error " + @ERROR + ": " + @SERROR
		EndIf
	EndIf
Next

Top
#171528 - 2006-12-14 07:27 PM Re: ,Adding/removing network printers [Re: Witto]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
one thing you could change though is that don't delete the old printer before the new one maps fine.

this way users don't get left without printers if say the new server for some reason is not reachable or you mistyped the printershare or something.

then, once you remove the old server there is only few if any users left without printers. instead all of them.
_________________________
!

download KiXnet

Top
#171529 - 2006-12-14 07:29 PM Re: ,Adding/removing network printers [Re: Dugster]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Here is an old post about this.

http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=37276

Top
#171670 - 2006-12-18 03:01 PM Re: ,Adding/removing network printers [Re: NTDOC]
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
I really appreciate all the advice and help you've given me here. I have managed to make the script work with one printer but have one *last* question for you about making the script work for multiple printers.

In the script Witto sent above there is the following entry:

$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs

I can't get this to work. If I just say $Ptr = "printername" and remove the $ArrPtrs bit, the script runs fine (with a couple of tweaks). Just wondering if I can make the multiple printers re-map in one script or if I should do a script for each printer?

Thanks a lot

Top
#171672 - 2006-12-18 03:40 PM Re: ,Adding/removing network printers [Re: Dugster]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Debug 101

When code isn't working, add lots of print statements to figure out what it's doing, and comment out the actual actions.

Code:
$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs
  'current PTR value is >' $Ptr '<' ?
  ; other code...
Next

Note the leading / trailing angle brackets - they precisely delimit the content of the variable, which will disclose any leading or trailing spaces as well as empty vars.

A lot of my code contains DBG('message >' + $Var + '<' + @CRLF) strings. the DBG function simply displays the message it receives IF the global $DEBUG variable is set:

Code:
Function Dbg($_Msg)
  If $DEBUG
    $_Msg
  EndIF
EndFunction


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

Top
#171675 - 2006-12-18 05:05 PM Re: ,Adding/removing network printers [Re: Glenn Barnas]
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
Its ok - I got it working. Thanks a lot for your time and help on this.
Top
#171703 - 2006-12-18 11:43 PM Re: ,Adding/removing network printers [Re: Dugster]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I hope you also implemented something like Jooel suggested:
Originally Posted By: Jooel

don't delete the old printer before the new one maps fine

I think this would be better, but again, I did not test it
Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $SO
$SO=SetOption("Explicit", "ON")
$SO=SetOption("NoMacrosInStrings", "ON")
$SO=SetOption("NoVarsInStrings", "ON")
$SO=SetOption("WrapAtEOL", "ON")

Dim $OldServer, $NewServer, $ArrPtrs, $Ptr, $PtrMapState, $NUL
$OldServer = "OldServer"
$NewServer = "NewServer"
$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs
	$PtrMapState = $PriMapState("\\" + $OldServer + "\" + $Ptr)
	If 0 < $PtrMapState			
		$NUL = AddPrinterConnection("\\" + $NewServer + "\" $Ptr)
		If NOT @ERROR AND 2 = $PtrMapState
			$NUL = SetDefaultPrinter("\\" + $NewServer + "\" $Ptr)
			If @ERROR
				? "Error setting default Printer Connection " + $Ptr
				? "Error " + @ERROR + ": " + @SERROR
			EndIf
		Else
			? "Error adding Printer Connection " + $Ptr
			? "Error " + @ERROR + ": " + @SERROR
		EndIf
		If NOT @ERROR
			$NUL = DelPrinterConnection($Ptr)
			If @ERROR
				? "Error deleting Printer Connection " + $Ptr
				? "Error " + @ERROR + ": " + @SERROR
			EndIf
		EndIf
	EndIf
Next

If AddPrinterConnection generates an error, or if AddprinterConnection succeeds but SetDefaultPrinter generates an error, DelPrinterConnection should not be executed.

Top
#171736 - 2006-12-19 05:48 PM Re: ,Adding/removing network printers [Re: Witto]
Dugster Offline
Fresh Scripter

Registered: 2006-12-12
Posts: 11
Thanks for that. I made a couple of changes to get this to work but basically you've helped a great deal. For info the working script is as follows:


Code:
If NOT @LOGONMODE
	Break On
Else
	Break Off
EndIf
Dim $SO
$SO=SetOption("Explicit", "ON")
$SO=SetOption("NoMacrosInStrings", "ON")
$SO=SetOption("NoVarsInStrings", "ON")
$SO=SetOption("WrapAtEOL", "ON")

Dim $OldServer, $NewServer, $ArrPtrs, $Ptr, $PtrMapState, $NUL
$OldServer = "OldServer"
$NewServer = "NewServer"
$ArrPtrs = Split("Printer1,Printer2,Printer3",",")
For Each $Ptr In $ArrPtrs

	$PtrMapState = PriMapState("\\" + $OldServer + "\" + $Ptr)
	If 0 < $PtrMapState			
			$NUL = AddPrinterConnection("\\" + $NewServer + "\" + $Ptr)
		If NOT @ERROR AND 2 = $PtrMapState
			$NUL = SetDefaultPrinter("\\" + $NewServer + "\" + $Ptr)
			If @ERROR
				? "Error setting default Printer Connection " + $Ptr
				? "Error " + @ERROR + ": " + @SERROR
			EndIf
		Else
			IF @ERROR
				? "Error adding Printer Connection " + $Ptr
				? "Error " + @ERROR + ": " + @SERROR
			EndIf	
		EndIf
		If NOT @ERROR
		
			$NUL = DelPrinterConnection("\\" + $OldServer + "\" + $Ptr)
			If @ERROR
				? "Error deleting Printer Connection " + $Ptr
				? "Error " + @ERROR + ": " + @SERROR
			EndIf
		EndIf
	EndIf

Next

If AddPrinterConnection generates an error, or if AddprinterConnection succeeds but SetDefaultPrinter generates an error, DelPrinterConnection should not be executed.[/quote]

Top
#173802 - 2007-02-09 04:15 PM Re: ,Adding/removing network printers [Re: Dugster]
LANSonic Offline
Lurker

Registered: 2007-02-08
Posts: 3
Dugster,
I would love to try this script, but I have 100 network printers to move to the new print server. How can I wrap the text in $AppPtrs declaration so I can include all 100 names? Would it be similar to this?
Code
____________________________________________________________________
$ArrPtrs = Split("Printer1,Printer2,Printer3, _
Printer4,Printer5,Printer6,Printer7,Printer8, _
Printer9",",")
____________________________________________________________________
End Code

Thanks in advance for your help.

Top
#173803 - 2007-02-09 04:25 PM Re: ,Adding/removing network printers [Re: LANSonic]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hi LANSonic, and welcome to the board.
You should review The Post/Reply Formatting Box and How to use it.

The use of the underscore is for vbscript, with KiXtart it would look like this:
 Code:
$ArrPtrs = Split("Printer1,Printer2,Printer3,
Printer4,Printer5,Printer6,Printer7,Printer8,
Printer9",",")
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#173804 - 2007-02-09 04:30 PM Re: ,Adding/removing network printers [Re: Benny69]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
LANSonic,
Maybe you can ask your questions about your topic in your own thread?
http://www.kixtart.org/forums/ubbthreads...true#Post173784
I am very sure everybody is very willing to answer you, as you can see.

Top
#173805 - 2007-02-09 04:32 PM Re: ,Adding/removing network printers [Re: Benny69]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
No need for Split() when you can populate an array directly.

$ArrPtrs = 'Printer1','Printer2','Printer3',
'Printer4','Printer5','Printer6','Printer7','Printer8',
'Printer9'

Affects the golf score though.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

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 560 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.035 seconds in which 0.013 seconds were spent on a total of 12 queries. Zlib compression enabled.

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