SeaSlug
(Fresh Scripter)
2007-01-11 12:29 AM
Using ini file to map printers

Hi all,

Noobie here, just learning about Kixtart

Would like some help please.

I need to map a whole bunch of network printers, close to 50. These printers do get updated, and all users need to connect to all of them!

when I run the addprinterconnection, it take a while to map them all, so I wanted to map them only once, but when the admin adds a new printer, they can add it to an ini file and simply update the version number.

So I would have an ini file looking something like

[Version]
Number=1.0

[Printers]
\\pserver\printer1
\\pserver\printer2
\\pserver\printer3
etc to 50

So the admin simply updates the version number both in the ini file and the printer.kix script, and if it is different than the one refered to in the script script, then re-add all the printers.

Likewise, will do the same when pritners are going to be deleted.

Does this make sense?

Any advice and code welcome!

Thank you


Les
(KiX Master)
2007-01-11 12:32 AM
Re: Using ini file to map printers

Quote:
Does this make sense?

No.

That is not a properlly formatted INI file. There are no = in the Printers section.


SeaSlug
(Fresh Scripter)
2007-01-11 12:39 AM
Re: Using ini file or basic file to map printers

so would I need something like:

[Version]
Number=1.0

[Printers]
1=\\pserver\printer1
2=\\pserver\printer2
3=\\pserver\printer3

taking a different tack, I am also thinking of keeping it simpler??

Have a file called printers.txt for example.
Have all the printers in here.

When logging on, check existing printer connections in the users registry hive.
Read the file entries

Somehow - not sure how compare the two and add or delete those that have been added to or deleted in the file.

Thanks


Les
(KiX Master)
2007-01-11 01:07 AM
Re: Using ini file or basic file to map printers

Well.. first with the version, I would not use a decimal point.
Second, the [Printers] section I would not have numbered. That way you can enum them.

[Version]
Number=1

[Printers]
\\pserver\printer1=1
\\pserver\printer2=1
\\pserver\printer3=1


SeaSlug
(Fresh Scripter)
2007-01-11 01:17 AM
Re: Using ini file or basic file to map printers

So how would I go about enumerating the [Printers] list and then comparing that with the current list stored in HKCU\Printers\Connections for the user?

thanks \:\)


Les
(KiX Master)
2007-01-11 01:32 AM
Re: Using ini file or basic file to map printers

Read in the PrinterPorts from the reg into an array.
Read in the Printers section from the INI into an array.

Search each element of one array against the next.


SeaSlug
(Fresh Scripter)
2007-01-11 01:48 AM
Re: Using ini file or basic file to map printers

Yes, that is exactly what I would like to do !!

So, I would create two arrays, then use ASCAN?

do you have any (simple) snippets of code I can use to begin better understanding how this is performed?

thanks in advance


Les
(KiX Master)
2007-01-11 02:17 AM
Re: Using ini file or basic file to map printers

Code:
Break on

$Index = -1
$RegKey = 'HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts'

Do
$Index = $Index + 1
$PrinterKeys = $PrinterKeys + EnumValue($RegKey, $Index)+CHR(10)
Until @Error

$ArrPrinterKeys = Split($PrinterKeys,CHR(10))


For each $Printer in $ArrPrinterKeys
	$Printer ?
Next
'Hit any key...'
Get $

$ArrPrinters = Split(ReadProfileString('c:\printers.ini','Printers',''),CHR(10))


For each $Printer in $ArrPrinters
	$Printer ?
Next


SeaSlug
(Fresh Scripter)
2007-01-11 02:42 AM
Re: Using ini file or basic file to map printers

Thanks Les

Working on it now.

Neglected to mention that I need to check the users remote printer connections, so am making the changes now.

what I do notice, is that the output from HKCU\Printers\Connections is ,,Server\Printer, yet my ini file is \\server\Printer.

How would I go about changing the reference?

Thanks in advance


SeaSlug
(Fresh Scripter)
2007-01-11 03:00 AM
Re: Using ini file or basic file to map printers

Ah,

When I create the array, I can do a split/join to replace the ,, with \

then continue doing the rest.

Is that correct ?

Thanks


Les
(KiX Master)
2007-01-11 03:24 AM
Re: Using ini file or basic file to map printers

Sure, only diff, that reg area has the names in keys not values so you need EnumKey() instead of EnumValue() but you probably already figured that out.

Les
(KiX Master)
2007-01-11 03:28 AM
Re: Using ini file or basic file to map printers

Code:
Break on

$Index = -1
$RegKey = 'HKEY_CURRENT_USER\Printers\Connections'

Do
$Index = $Index + 1
$PrinterKeys = $PrinterKeys + Join(Split(EnumKey($RegKey, $Index)+CHR(10),','),'\')
Until @Error

$ArrPrinterKeys = Split($PrinterKeys,CHR(10))


For each $Printer in $ArrPrinterKeys
	$Printer ?
Next
'Hit any key...'
Get $

$ArrPrinters = Split(ReadProfileString('c:\printers.ini','Printers',''),CHR(10))


For each $Printer in $ArrPrinters
	$Printer ?
Next


SeaSlug
(Fresh Scripter)
2007-01-11 04:41 AM
Re: Using ini file or basic file to map printers

Hi Les,

Yeap figured that one out with the keys/value.

What I couldn't figure was the split, join. I had it in the wrong spot

So far so good.

Last step, how do I go about comparing between the two arrays?
Would I use ASCAN and check for the existance of the variable from the ini file against the registry array?

thanks for all your help so far!


Les
(KiX Master)
2007-01-11 05:30 AM
Re: Using ini file or basic file to map printers

Well, this should compare the INI to the reg and add it if missing.
Code:
For each $Printer in $ArrPrinters
	$Printer ?
	If AScan($ArrPrinterKeys,$Printer) = -1
		$RC = AddPrinterConnection($Printer)
	EndIf
Next

You still need to do the same thing the other way around comparing the reg to the INI to delete what is not in the INI.

Remember this this is all optimistic code. You still need to add error checking etc.


SeaSlug
(Fresh Scripter)
2007-01-11 05:49 AM
Re: Using ini file or basic file to map printers

I managed to get something similiar going, though nowhere as elegant as yours.

I will incorporate your code into my script to make it look a bit better. Will help the admins out greatly that they only have to change 1 file to update the printers.

I agree with your logging comments, once I get the basic theory going, then I will work on the logging.

I can't thank you enough for helping me out
BIG Double thumbs up to ya !! Your a legend !

BTW - what does the $RC = do ? Does that catch any output?

Thanks again.


SeaSlug
(Fresh Scripter)
2007-01-11 06:26 AM
Re: Using ini file or basic file to map printers

I noticed that when I do my check for the registry array contents against the ini file array, there appears to be 2 blank lines (cr/lf perhaps) that cause 2 errors to occur.

Any ideas?


Les
(KiX Master)
2007-01-11 07:39 PM
Re: Using ini file or basic file to map printers

The $RC does just that, capture the return code that would otherwise fall out onto the floor. As I said, very optimistic code simply to demonstrate the basics. You will need to handle the null array elements.