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
Solved

VBS print script based on client ip

Posted on 2011-03-14
6
341 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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