Solved

VBS print script based on client ip

Posted on 2011-03-14
6
356 Views
Last Modified: 2014-01-09
Hello,

I need a vbs script to map printers to clients based on the subnet.
The ideal way would be to have a .ini file where all the printers and subnets are defined, so the script can read from that.

.INI file:

 
[172.16]
\\sfjhsprint\lx-utred
\\sfjhsprint\lx-hs3
[172.17.1]
\\sfjhsprint\lx-rehab-lj
[172.17.84]
\\sfjhsprint\prtKilen2
[172.18.54]
\\sfjadmprint\lx-virikskole
[10]
\\sfjadmprint\prtpers
\\sfjadmprint\lx-avgift

Open in new window


and so on...

The only code i got so far, is the one to find the client ip-address:

strcomputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
   
strCount = 1

For Each objitem in colitems
    If strCount = 1 Then
        strIPAddress = Join(objitem.IPAddress, ",")
        IP = stripaddress
        strCount = strCount + 1
        wscript.echo IP
    Else
    End If

next
0
Comment
Question by:jorgensen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 250 total points
ID: 35128546
Hi jorgensen

You could use something like this, it doesn't use a INI file but gives you an idea how to go about it, I use a similar one for our network printers.

Jawa29
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set cIP = oWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IP in cIP
    If Not IsNull(IP.IPAddress) Then 
        For i=LBound(IP.IPAddress) to UBound(IP.IPAddress)
            IPAddress = IP.IPAddress(i)
        Next
    End If
Next

Set oNetwork = CreateObject("WScript.Network")

Select Case Split(IPAddress, ".")(0)
	Case "10"
		oNetwork.AddWindowsPrinterConnection "\\printserver\printer"
	Case "172"
		Select Case Split(IPAddress, ".")(1)
			Case "16"
				oNetwork.AddWindowsPrinterConnection "\\printserver\printer"
			Case "18"
				oNetwork.AddWindowsPrinterConnection "\\printserver\printer"
			Case "17"
				Select Case Split(IPAddress, ".")(2)
					Case "1"
						oNetwork.AddWindowsPrinterConnection "\\printserver\printer"
					Case "84"
						oNetwork.AddWindowsPrinterConnection "\\printserver\printer"
				End Select
		End Select
End Select

Open in new window

0
 
LVL 3

Expert Comment

by:sam0x01
ID: 35133474
You could simplify the script especially if you have subnets that are smaller than a Class C, by comparing the default gateway instead.
0
 

Author Comment

by:jorgensen
ID: 35135544
Thanks for the respons jawa29.

The only problem i can see with that script is that i have 300+ printers, so it can easily get messy and hard to maintain. But now i got more to work with!

0
 
LVL 3

Assisted Solution

by:sam0x01
sam0x01 earned 250 total points
ID: 35143397
Hi,

Please try the code below.

It reads a file of the format specified in the question and will map the printers. Post any issues you get here, but I have performed some rough testing on it
Option Explicit
Dim oFSO, oFile, sLine, bMatched, sMyIP
Const iForReading = 1
Const sPrintersFile = "C:\Temp\Printers.txt"
'Uncomment next line if you do not want users to receive potential errors
' On Error Resume Next
'------------------------------------------------------------------------
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile (sPrintersFile, iForReading)
sMyIP = GetMyIP()
bMatched = False
Do Until oFile.AtEndOfStream
    sLine = oFile.Readline
	If (Left(sLine,1) = "[") Then
		If bMatched Then
			'WScript.Echo "Bye"
			Exit Do
		Else
			bMatched=MatchIPStr(sLine,sMyIP)
		End If
	Else
		If bMatched Then
			MapNetworkPrinter(sLine)
		End If
	End If
	
Loop

If Not bMatched Then
	' Code if there are no printers in the file
	' for the subnet
End If

Sub MapNetworkPrinter (sPath)
	Dim oNetwork
	Set oNetwork = CreateObject("WScript.Network")
	oNetwork.AddWindowsPrinterConnection sPath
End Sub

Function MatchIPStr(sLine, sIP)
	Dim iLineLen,sIPPat, iIPPatLen

	bMatched = False
	iLineLen = Len(sLine)
	sIPPat = Left(sLine, iLineLen-1)
	sIPPat = Right(sIPPat, iLineLen-2)
	
	iIPPatLen = Len(sIPPat)
	WScript.Echo """" & sIPPat & """, """ & Left(sIP,iIPPatLen) & """"
	If Left(sIP,iIPPatLen) = sIPPat Then
		bMatched = True
		'WScript.Echo "Matched"
	End If
	MatchIPStr = bMatched
End Function

Function GetMyIP
	Dim oWMIService, cIP, IP, i, IPAddress
	Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

	Set cIP = oWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
	 
	For Each IP in cIP
		If Not IsNull(IP.IPAddress) Then 
			For i=LBound(IP.IPAddress) to UBound(IP.IPAddress)
				IPAddress = IP.IPAddress(i)
			Next
		End If
	Next
	GetMyIP=IPAddress
End Function

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question