Page 1 of 1 1
Topic Options
#210387 - 2015-07-15 12:02 AM Mapping printers using ini file
AshishS Offline
Fresh Scripter

Registered: 2008-10-27
Posts: 20
Hello everyone,
I have very basic understanding of Kix so looking at everyone's examples here, I'm trying to map printers using ini file but no having much luck. I want my kix script to read an ini file and map the printers based on the content. All of our computer names start with CAMC followed by the physical location of the PC. Example CAMCER01 means the PC is located in ER.

My ini file looks like this:

camcamb12=\\camcprntsrv2\Ambulatory: POD2
camcamb14=\\camcprntsrv2\Ambulatory: POD4

Here is the code in my kix scritp:
-------------------------------------------------------------------------------
$Location = @WKSTA
$PrinterTable = "\\CAMCAPPSRV01\Printers\PrintersList.ini"
$Printer = ReadProfileString($PrinterTable, AddPrinterConnection, $Location)

-------------------------------------------------------------------------------------

I got the above ini file & code from one of the posts on this website. I'm really not clear why or how $Location is needed in my ReadProfileString area. Also, with me declaring AddPrinterConnection, shouldn't it just read the ini file...look at @WKSTA and map the printer.

Thanks in advance for your help.

Top
#210388 - 2015-07-15 01:31 AM Re: Mapping printers using ini file [Re: AshishS]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4447
Loc: USA
You got two functions intertwined incorrectly there, readprofilestring and addprinterconnection. Until you get a better grip on kix I suggest you separate the functions and lines to make it completely clear what you are doing.

Readprofilestring - http://www.kixtart.org/?p=commandRef#_Toc146265782
AddPrinterConnection - http://www.kixtart.org/?p=commandRef#_Toc146265719

 Code:
$Dept=left(right(@wksta,-4),-2)
$Printer01=readprofilestring($NameofINI,$Dept,"Printer01")
$RC=AddprinterConnection($Printer01)


Assuming that all computers start with the 4 letters, and end with 2 numbers, $Dept=left(right(@wksta,-4),-2) will get the dept.

The Section Value of the INI would use the $Dept from above. The INI would look something like:

[AMB]
Printer01=\\camcprntsrv2\Ambulatory: POD2
Printer02=\\camcprntsrv2\Ambulatory: POD4

[ER]
Printer01=\\server\Printer ShareName1
Printer02=\\server\Printer ShareName2

So...
$Printer01=readprofilestring($NameofINI,$Dept,"Printer01")
should get the first printer based on Dept, then
$RC=AddprinterConnection($Printer01)
adds the printer.

This gets more a little tricky with multiple printers, but its not hard. Digest this and come back with more questions.





Top
#210389 - 2015-07-15 01:33 AM Re: Mapping printers using ini file [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4447
Loc: USA
BTW... the format of the INI is completely up to you. This is just a suggestion.
Top
#210390 - 2015-07-15 06:33 PM Re: Mapping printers using ini file [Re: Allen]
AshishS Offline
Fresh Scripter

Registered: 2008-10-27
Posts: 20
@Allen,
Thank you for your help. I'm following what you are saying. So what I want to do is add a printer or printers based on PC name. I don't want all the PCs in the dept to be mapped to all the printers in the dept either. I just want the script to read my ini file, look at the PC name and based on what it reads, map the printer that is in ini file.

So if I do

$Dept = @wksta should get me the PC name

and my ini file looks like this:

camcamb12=\\camcprntsrv2\Ambulatory: POD2
camcamb14=\\camcprntsrv2\Ambulatory: POD4


So then my kix would look like:

$Printer01=readprofilestring($NameofINI,$Dept,"camcamb12")
$RC=Addprinterconnection($camcamb12)

So if the code above is correct, the script should map PC name camcamb12 to printer named Ambulatory: POD2 . Is that correct?

If so, then in my ini file, I would have to write down each PC name that needs certain printer attached.

Please let me know if I'm following the right train of thought here.


Edited by AshishS (2015-07-15 07:17 PM)

Top
#210391 - 2015-07-15 07:02 PM Re: Mapping printers using ini file [Re: AshishS]
AshishS Offline
Fresh Scripter

Registered: 2008-10-27
Posts: 20
@Allen,

Here is what my ini file looks like:

[CAMCAMB12]
camcamb12=\\camcprntsrv2\Ambulatory: POD 2

[CAMCAMB14]
camcamb14=\\camcprntsrv2\Ambulatory: POD 4
----------------------------------------------------

Here is my code:
$Location = @WKSTA
$PrinterTable = "\\CAMCAPPSRV01\Microsoft\Printers\PrintersList.ini"
$Printer01 = ReadProfileString($PrinterTable, $Location, "Printer01")
$RC = AddPrinterConnection($Printer01)
--------------------------------------------------------------------

So no matter what, it is not mapping any of the printers from the ini file to the PC names. What am I missing here?


Edited by AshishS (2015-07-15 07:36 PM)

Top
#210393 - 2015-07-15 08:28 PM Re: Mapping printers using ini file [Re: AshishS]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4285
Loc: New Jersey
Lets assume that the computer name is "CAMCAMB12" and you have the INI file as shown above.

$Location='CAMCAMB12' ; defined in code by @WKSTA
$Printer01=ReadProfileString($PrinterTable, $Location, 'Printer01")

At this point, $Printer01 is blank, because there is nothing in your INI file in the CAMCAMB12 section that has a "Printer01" value.

Consider this format for your INI file:
[COMPUTERNAME]
Printer01=UNC Path to a printer
Printer02=UNC Path to a different printer

NOW - when you read the "Printer01" value from the computer section, you will actually get data. I'm not sure what the ": POD #" is, but it doesn't look like a legit printer name (Not sure you can use a colon!) The value after "Printer01" should JUST BE the UNC path, nothing else. If you need additional info, like a description or something, use a different value name, like "Printer01_Desc".

You can use a loop to read Printer01 through - say - Printer05. The first time you get an empty result, you exit the loop. This allows an arbitrary number of printers to be mapped to each host.
 Code:
For $I = 1 to 5
  $PtrID   = 'Printer0' + $I
  $Printer = ReadProfileString($PrinterTable, @WKSTA, $PtrID)
  If $Printer = ''
    $I = 999      ; no more printers - exit the loop
  Else            ; map the printer and then check for more
    $Rc = AddPrinterConnection($Printer)
  EndIf
Next
This is untested, but should provide the right ideas...

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

Top
#210395 - 2015-07-15 11:29 PM Re: Mapping printers using ini file [Re: Glenn Barnas]
AshishS Offline
Fresh Scripter

Registered: 2008-10-27
Posts: 20
@Glenn,
Thanks for the feedback. Using your approach, I made the kix script work.

So if I want to add multiple printers to one computer, I have to use a loop...correct.

So far I'm doing one printer per PC so the script is working.

Also, to answer your question about ":POD #", that is the share name and printer name on my print server.

Big thanks to Allen & Glenn for helping me. Really appreciate it guys.

Top
#210398 - 2015-07-17 08:12 AM Re: Mapping printers using ini file [Re: AshishS]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22333
Loc: OK
if you look at Glenn's code again, he already has the loop written on silver platter for you. He limits it to 5 printers per workstation, but that is enough for most situations.
_________________________
!

download KiXnet

Top
#210423 - 2015-07-29 12:19 AM Re: Mapping printers using ini file [Re: Lonkero]
AshishS Offline
Fresh Scripter

Registered: 2008-10-27
Posts: 20
I just tried using the loop for the first time today and for PC name CAMCACT07, it is only mapping one printer and completely ignoring the 2nd printer. Its like it is not reading the second line in the loop. Any help would be greatly appreciated guys. Thanks in advance.

My ini file:

[CAMCRAD07]
Printer01=\\camcprntsrv2\Radiology - Ultrasound

[CAMCACT07]
Printer01=\\camcprntsrv2\Accounting - Accounts Payable
Printer02=\\camcprntsrv2\Accounting - Color Printer

----------------------------------------------------------------

My Kix:

;READ INI FILE TO MAP PRINTERS
$Location = @WKSTA
$PrinterTable = "\\CAMCAPPSRV01\Microsoft\Printers\PrintersList.ini"

For $I = 1 to 5
$PtrID = 'Printer0' + $I
$Printer = ReadProfileString($PrinterTable, @WKSTA, $PtrID)
If $Printer = ''
$I = 999 ; no more printers - exit the loop
Else ; map the printer and then check for more
$Rc = AddPrinterConnection($Printer)
EndIf
Next



Edited by AshishS (2015-07-29 12:24 AM)

Top
#210424 - 2015-07-29 12:52 AM Re: Mapping printers using ini file [Re: AshishS]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4447
Loc: USA
Some basic output will likely get you in the right direction.


 Code:
;READ INI FILE TO MAP PRINTERS
$Location = @WKSTA
$PrinterTable = "\\CAMCAPPSRV01\Microsoft\Printers\PrintersList.ini"

? @kix
? @wksta

For $I = 1 to 5
  $PtrID = 'Printer0' + $I
  $Printer = ReadProfileString($PrinterTable, @WKSTA, $PtrID)
  If $Printer = ''
    $I = 999 ; no more printers - exit the loop
  Else ; map the printer and then check for more
    ? $PtrID + ":" + $Printer
    $Rc = AddPrinterConnection($Printer)
    ? @serror
    ?
  EndIf
Next

Top
#210425 - 2015-07-29 01:50 AM Re: Mapping printers using ini file [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4285
Loc: New Jersey
Having debug messages is so critical to seeing what the code is doing (as opposed to what you THINK it SHOULD be doing! :p ) that I've written a function to handle this, called fMsg(). I use it for almost all output, especially debug messages, because by setting the $DEBUG value on or off, I can control whether the messages display or not/
 Code:
Global $DEBUG
$DEBUG = 1        ; turn debug on

;Simple output message
fMsg('This is my message to you...')

; Output ONLY when debug is enabled
fMsg('The XX variable contains: ' + $XX, '', 0, 4)

; display an error and quit running with error code 54, put more detail in the log
fMsg('Oops - something went terribly, horribly wrong!', 'Error 54 in block 7', 0, 8, 54)
With $DEBUG = 1, I get all my debug messages, and with it set to 0, they are suppressed. The syntax of this function is
fMsg(display_text, [Log_text], Suppress_LF, Method, Special)
The first parameter is the message, and is required. Log_Text lets you write one thing to the screen and something else to the log. Suppress_LF will suppress the newline if true. Method is a binary weighted value that controls how the message is output or how the function operates. Special is the exit code to use for an Abend method.

You can download the UDF from my web site's KiXtart Function Library. The advantage of this is that you don't have to go back and remove the debug code. You never know when it will come in handy again!

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

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 36 anonymous users online.
Newest Members
thequeen, ameliaryan, shalomsalami, dkdssdk, Vroon
17639 Registered Users

Generated in 0.041 seconds in which 0.017 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