Page 2 of 2 <12
Topic Options
#191846 - 2009-01-20 05:55 AM Re: Assign printers based on IP address of client? [Re: lejeje]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Welcome!

Looking at your code, there's no statement to actually map the printer. It looks like you grabbed the example that tried to illustrate the concept, but didn't actually map. Are you getting a proper message "Mapping printer xxxx" ? If so, the logic is working. You'll need to add the command to actually map the printer at that point.

You should read the post(s) that follow the one where you copied the code from - they actually contain the commands to map the printer (AddPrinterConnection) and are more complete, even though they use an INI file.

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

Top
#195486 - 2009-08-20 05:42 PM Re: Assign printers based on IP address of client? [Re: Glenn Barnas]
getelevated Offline
Fresh Scripter

Registered: 2009-08-20
Posts: 8
Loc: Boston
Hello Glenn,

First off, thanks for this script. It was very helpful for a customer office move that we just went through. We put the script together and during our testing all of the printers were installed flawlessly on each subnet. But now that the move has been completed there is one strange issue. When the users first log on, the script starts to run and the "No printers were mapped!" comment appears. But the users should have received at least one. Now here's the tricky part; if I manually run the kix script when the user is logged in (going to the sysvol share via command line) the printers get mapped correctly. Any ideas?

Here is the script, stating with the select state for the AD site that this office is in. Everything works but the printer install on login:
 Code:
CASE @Site = "ADSITE"
  ? "Logging in from Site"

   WriteValue("Software\Microsoft\Windows\CurrentVersion\Internet Settings", "MaxConnectionsPerServer", "6", "REG_DWORD")

 ; Map S drive for application
 use s: "\\DC1\Applications\APPS"


   SELECT

   CASE INGROUP("Facilities")
    use g: "\\DC2\Facilities"

   CASE INGROUP("Technology")
    use g: "\\DC2\Tech"

   CASE INGROUP("Call Center")
    use g: "\\DC2\TSR"

   CASE INGROUP("Finance")
    use g: "\\DC2\Finance"

   CASE INGROUP("Operations")
    use g: "\\DC2\Operations"

   CASE INGROUP("Human Resources")
    use g: "\\DC2\HumanResources"

   CASE INGROUP("Employee Development")
    use g: "\\DC2\Corporate Training"

   CASE INGROUP("Marketing")
    use g: "\\DC2\MKT"

   ENDSELECT

 IF INGROUP("Group1")
    use U: "\\server\drive"
 ENDIF

 IF INGROUP("Group1")
    use I: "\\DC2\Intranet"
    use w: "\\Intranet\Documents"
 ENDIF
  
 IF INGROUP("Case Files")
   use r: /delete
   use r: "\\server6\share"
 ENDIF


; Copy file to user directory to run script once

IF EXIST ("%appdata%\Microsoft\printers.txt") 
	; do nothing
  ELSE
   copy "\\DC2\PrintTemp$\printers.txt" "%appdata%\Microsoft\printers.txt" /c
   sleep 2

; Delete all existing Network Printer Connections

  function DelPrinterConnections()
   dim $c,$bk,$conn
   $c=0
   $bk="HKEY_CURRENT_USER\Printers\Connections"
   $conn=enumkey($bk,$c)
   while @error=0
    $c=$c+0
    $conn=delkey($bk+"\"+$conn)
    $conn=enumkey($bk,$c)
   loop
  endfunction

DelPrinterConnections()


; Install new printers based on workstation subnet

; Break On
; Uses an INI file for subnet/printer mapping
; The INI file in this example is called NetPrint.ini, and uses one section
; called MAPPINGS. Each value consists of a comma-delimited pair of data
; representing the network/mask and printer share UNC in the format
; sitename=w.x.y.z/mask,\\server\printer

$Mapped = 0					; value showing that printer was not mapped

$MyIP = Join(Split(@IPADDRESS0, ' '), '')	; Get the local IP address, removing spaces

; Load an array from the INI file
$Mappings = EnumIni('.\netprint.ini', 'MAPPINGS')

For Each $SiteName in $Mappings

  ; create a 2-element array by splitting the data at the comma
  ; element 0 contains the network/mask, and element 1 contains the printer UNC
  $NetPrint = Split(ReadProfileString('.\netprint.ini', 'MAPPINGS', $SiteName), ',')

  ; see if the local IP is inside the network
  If InSubnet($MyIP, $NetPrint[0])

    ; these print statements are for diagnostic/testing
 ?  'My ip address (' $MyIP ') exists in the ' $SiteName ' site (' $NetPrint[0] ')' ?
 ?   'Mapping printer ' $NetPrint[1] ?

    ; make the printer connection
    AddPrinterConnection($NetPrint[1])

    ; show that at least 1 printer mapped successfully
    If Not @ERROR
      $Mapped = 1
    EndIF

  EndIF

Next


If Not $Mapped
 ? 'No printers were mapped!' ?
EndIf

; do not modify anything after this line!!!

;; 
;;====================================================================== 
;; 
;;FUNCTION       InSubnet() 
;; 
;;ACTION         Determines if a specific IP address is in a subnet 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 / 2008/01/20 
;; 
;;SYNTAX         InSubnet(Address, Network) 
;; 
;;PARAMETERS     Address - REQUIRED, IP address in dotted-decimal (w.x.y.z) 
;; 
;;		 Network - REQUIRED, Network in CIDR format (w.x.y.z/mask) 
;; 
;;REMARKS         
;; 
;;RETURNS        1 if ip address is in the defined subnet, 0 otherwise 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista, X64 
;; 
;;EXAMPLES       If InSubnet('192.168.12.243', '192.168.8.0/21') 
;;		   'Is in subnet!' ? 
;;		 EndIf 
; 
Function InSubnet($_IPAddr, $_Network)
 
  Dim $_aAdd				; array of address values 
  Dim $_Mask				; Mask value 
  Dim $_Hosts				; hosts in subnet 
 
  ; Convert the supplied IP address to a double value representing the decimal address 
  $_aAdd = Split($_IPAddr, '.')
  $_IPAddr = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)
 
  ; Convert the network from a w.x.y.z/mask format to a decimal value 
  ; this is the starting network address value 
  $_Network = Split($_Network, '/')
  $_Mask = Val($_Network[1])
  $_aAdd = Split($_Network[0], '.')
  $_Network = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)
 
  ; Set the number of hosts in the defined network 
  $_Hosts = 1.0
  For $_I = 31 to $_Mask Step -1
    $_Hosts = ($_Hosts * 2.0)
  Next
 
  ; return the value  
  $InSubnet = 0
  If $_IPAddr >= $_Network And $_IPAddr < ($_Network + $_Hosts)
    $InSubnet = 1
  EndIf
 
  Exit 0
 
EndFunction
 



;; 
;;====================================================================== 
;; 
;;FUNCTION       EnumIni() 
;; 
;;ACTION         Enumerates sections or keys of an INI file 
;; 
;;AUTHOR         Glenn Barnas  
;; 
;;VERSION        2.0 
;; 
;;DATE CREATED   2003/11/17 
;; 
;;DATE MODIFIED  2004/10/16 
;; 
;;SYNTAX         EnumIni(File [, Section]) 
;; 
;;PARAMETERS     File - 	REQUIRED, path/name of INI file to examine 
;; 
;;               Section -	OPTIONAL, Section name to parse 
;; 
;;REMARKS        Returns an array containing the sections in an INI file, or 
;;               an array of key names in a specified section. Errors are returned 
;;               for non-existant files or INI file reads. If the specified file 
;;               contains no sections, or the specified section contains no keys, 
;;               the UDF exits with error 13 and returns a null array. Thus, a For-Each loop 
;;               will properly perform no iterations if no data is returned. 
;; 
;;               CAUTION - Error 13 is returned for empty files, or nonexistant sections. 
;;                         This is not necessarily a "failure". 
;; 
;;RETURNS        Array of sections or keys in a section 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    Kix 4.2+, NT4, W2K, WXP, W2K3 
;; 
;;EXAMPLES       $Sections = EnumIni('.\config.ini') 
;;               $Keys = EnumIni('.\config.ini', 'Common') 
; 
Function EnumIni($_fSrcFile, OPTIONAL $_fSectName)
 
  Dim $_fSectList
 
  ; die if the file doesn't exist 
  If Exist($_fSrcFile) = 0
    Exit 2
  EndIf
 
  ; Get the list of sections or keys 
  $_fSectList = ReadProfileString($_fSrcFile, $_fSectName, '')
  ; Return if error occurred 
  If @ERROR
    Exit @ERROR
  EndIf
 
  ; If len is >0, return an array of sections 
  ; If len is 0, either no sections or keys exist, or an invalid section name was specified. Return nothing. 
  If Len($_fSectList) > 0
    $EnumIni = Split(Left($_fSectList,len($_fSectList)-1), Chr(10))
    Exit 0
  EndIF
 
  ; return an error here for value not found (no sections or no keys in section) 
  Exit 13
 
EndFunction 

ENDIF


Edited by Mart (2009-08-20 05:46 PM)
Edit Reason: Added code tags

Top
#195489 - 2009-08-20 05:50 PM Re: Assign printers based on IP address of client? [Re: getelevated]
Mart Moderator Offline
KiX Supporter
*****

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

Two things:

-1 Please use the code tags when posting code.
-2 Please start your own thread and do not hijack this one. If needed include a reference to this thread in your post.

INFO: Moderator's message to new forum users


Edited by Mart (2009-08-20 05:50 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#195490 - 2009-08-20 06:24 PM Re: Assign printers based on IP address of client? [Re: Mart]
getelevated Offline
Fresh Scripter

Registered: 2009-08-20
Posts: 8
Loc: Boston
Will do, thanks Mart.
Top
Page 2 of 2 <12


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

Who's Online
0 registered and 194 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.069 seconds in which 0.037 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