|
|
|||||||
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 |
||||||||
|
|
|||||||
Quote: Does this make sense? No. That is not a properlly formatted INI file. There are no = in the Printers section. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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! |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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? |
||||||||
|
|
|||||||
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. |