Solved

VBS print script based on client ip

Posted on 2011-03-14
6
317 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
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now