Page 1 of 1 1
Topic Options
#188739 - 2008-07-14 07:37 PM Map Network Printers based on Partial Computer Name
jim4flyn Offline
Just in Town

Registered: 2008-07-14
Posts: 2
I need to create a script to assign network printers to certain rooms in different buildings. All of our computers are named based on their location. For example for a building I have called BE in room number 105 the first computer would be named be_105_1 and the second be_105_2, etc... I would like to be able to create a script to map a certain printer to all computers with a computer name beginning be_105. Is this possible using a Kix Script?

If not I could alternatively base the script off an OU that houses computer accounts or a domain group that computer accounts are assigned to. So far the only samples I have been able to find though are based on user group memberships, and that will not work in our situation.

Any suggestions or code sample would be appreciated.




Edited by jim4flyn (2008-07-14 07:38 PM)

Top
#188740 - 2008-07-14 07:47 PM Re: Map Network Printers based on Partial Computer Name [Re: jim4flyn]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Hi and welcome to the board.

What you want can be done very easily with a kix script.
A small example:

 Code:
If Left(@WKSTA, 6) = "be_105"
	;Delete the printer.
	$rc = DelPrinterConnection("printer_for_room_BE")
	;Connect the printer.
	$rc = AddPrinterConnection("\\printerserver\printer_for_room_BE")
	;Set the printer as default.
	$rc = SetDefaultPrinter("printer_for_room_BE")
Else
	;Do nothing.
EndIf 


Off course there are other ways you can walk. One of them is to use a Select - EndSelect statement.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#188741 - 2008-07-14 07:51 PM Re: Map Network Printers based on Partial Computer Name [Re: jim4flyn]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jim,

Do these rooms have specific IPs? If so..
 Code:
$ip=join(split(@ipaddress0,' '),'')
if LEFT($ip,6)='10.105'
; map a printer
endif

or..
 Code:
$ip=join(split(@ipaddress0,' '),'')
SELECT
case LEFT($ip,6)='10.105'
; map printer 105
case LEFT($ip,6)='10.106'
; map printer 106
;etc.
ENDSELECT


HTH,

Kent


Edited by Kdyer (2008-07-14 07:52 PM)
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#188743 - 2008-07-14 08:00 PM Re: Map Network Printers based on Partial Computer Name [Re: jim4flyn]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
A method that requires no future coding would be to utilize an INI file. If your computers use a standard name format [ \:D ] then you could take the common part of the name with the Left() function. Thus, if the first 6 characters of a computer name uniquely identified the site, you could
$Location = Left(@WKSTA, 6)
to get the Site ID.
An INI file could exist on a central file server, so it only needs to be maintained in one place. Using an INI file allows you to add or change mappings without having to modify and redistribute your code each time you update your configuration.

Using an ini file similar to this :
 Code:
[PrinterMapping]
be_105=\\server\printshare
be_106=\\server\printshare
(and so on...)

The code to support this would be
 Code:
$Location = Left(@WKSTA, 6)
; printer table is in \\fileserver\share\printertable.ini
; If this is done during a login script, table would be
; \\domain\netlogon\printertable.ini
$Table   = '\\fileserver\share\printertable.ini'
$Printer = ReadProfileString($Table, 'PrinterMapping', $Location)
; use your favorite method to map $Printer

You could do your assignments by OU as well, although that would require more coding. The login script on my web site does mapping by group or OU membership - might give you some ideas if the INI file doesn't give you what you want.

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

Top
#188744 - 2008-07-14 08:04 PM Re: Map Network Printers based on Partial Computer Name [Re: Kdyer]
jim4flyn Offline
Just in Town

Registered: 2008-07-14
Posts: 2
Thank for your reposes on that. I believe the first example will do what I need. The computers do not have specific IPs based on the room.
Top
#188748 - 2008-07-14 10:26 PM Re: Map Network Printers based on Partial Computer Name [Re: jim4flyn]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.

a bit late, but just for fun (merged the ideas presented);
(NOTE - THIS NEEDS CHECKING (ISN'T EVEN DONE YET :P )
 Code:
;need to dim vars
Dim $rc

Dim $Location,$Table
Dim $iproom,$ip
Dim $Printer,$DefaultPrinter

Dim $MissingIPprintermapp,$FileShareError
;get the "location"
$Location = Left(@WKSTA, 6)
; printer table is in \\fileserver\share\printertable.ini
; If this is done during a login script, table would be
; \\domain\netlogon\printertable.ini
$Table   = '\\fileserver\share\printertable.ini'

;logic should be if cannot read the file, get the error,
if NOT $Printer = ReadProfileString($Table, 'PrinterMapping', $Location)
	@SERROR = $FileShareError
	else
;else let's set the default printer from it.
	$DefaultPrinter=ReadProfileString($Table,'PrinterMapping',split(join$;not done. no brain left. (get the name of the printer)
endif

;now, let's check the room and ip
$iproom=ReadProfileString($Table,'IP-Check',$Location)
$ip=join(split(@ipaddress0,' '),'')

Select

;this case captures if there was an error getting the file
case $FileShareError
	'Error! Please report following to you administrator: File share error, ' + @CRLF + $FileShareError ?

;this case catches if the ip and room is a missmatch,
;and could provide the user with the settings to mapp it by "hand"
case $iproom =! $ip
	'Something seems to be wrong, contact your administrator,'?
	or please enter the name for the printer: ' gets $MissingIPprintermapp ?
	$MissingIPprintermapp=rtrim(ltrim($MissingIPprintermapp))
	if $MissingIPprintermapp
	$rc = AddPrinterConnection('\\printerserver\'+$MissingIPprintermapp)
		if $rc
		$rc = SetDefaultPrinter($MissingIPprintermapp) ' Setting your printer of choise as default ' ?
		else 'error occured! ' + @SERROR + ' ' + @ERROR + ' ' ?
		endif
	endif
;the nice "default case". If everything worked out, do the mapping
;according to the .ini .
case 1
	'Setting up your printer per-default configuration ' ?
	If Left(@WKSTA, 6) = $Printer
		if NOT $rc = SetDefaultPrinter($DefaultPrinter)
		$rc = AddPrinterConnection($Printer)
	        ;Delete the printer.
		$rc = DelPrinterConnection($DefaultPrinter)
		;Connect the printer.
		$rc = AddPrinterConnection($Printer)
		;Set the printer as default.
        	$rc = SetDefaultPrinter($DefaultPrinter)
		endif
        endif
	
	Else
		;Do nothing.
	EndIf 

EndSelect

/* the ini-file structure

[IP-Check]
be_105=10.105
be_106=10.106

[PrinterMapping]
be_105=\\server\printshare
be_106=\\server\printshare

*/
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

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 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.059 seconds in which 0.026 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