[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

VBS print script based on client ip

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
jorgensen
Asked:
jorgensen
  • 2
2 Solutions
 
jawa29Commented:
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
 
sam0x01Commented:
You could simplify the script especially if you have subnets that are smaller than a Class C, by comparing the default gateway instead.
0
 
jorgensenAuthor Commented:
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
 
sam0x01Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now