jim4flyn
(Just in Town)
2008-07-14 07:37 PM
Map Network Printers based on Partial Computer Name

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.




Mart
(KiX Supporter)
2008-07-14 07:47 PM
Re: Map Network Printers based on Partial Computer Name

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.


Kdyer
(KiX Supporter)
2008-07-14 07:51 PM
Re: Map Network Printers based on Partial Computer Name

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


Glenn BarnasAdministrator
(KiX Supporter)
2008-07-14 08:00 PM
Re: Map Network Printers based on Partial Computer Name

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


jim4flyn
(Just in Town)
2008-07-14 08:04 PM
Re: Map Network Printers based on Partial Computer Name

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.

Björn
(Korg Regular)
2008-07-14 10:26 PM
Re: Map Network Printers based on Partial Computer Name


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

*/