Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBS print script based on client ip

Posted on 2011-03-14
6
Medium Priority
?
369 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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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