VBS Printer Login Script Based on IP

illmaticone

OSNN Junior Addict
Joined
30 Jan 2006
Messages
45
hello,

im trying to create a login script that will install the proper printers based on your ip

currently we use two ip ranges

192.168.100.0
and
192.168.0.0

right now im trying to get this to work with 1 network, then i will introduce the second network once i get this working

currently the scrip works but only if it compares to 1 ip, cant figure out how to have it check,, idealy i would like it to do this..

compare ip to 192.168.100.* or 192.168.0.* or what ever the case maybe.
Code:
Dim strComputer
Dim objWMIService
Dim colItemSet
Dim colItem
Dim i
Dim wNetwork
Dim printerPath
Dim printerPath2
Dim printerPath3
Dim printerPath4

    strComputer = "."
   
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    On Error GoTo 0
   
    If Not (objWMIService Is Nothing) Then
        Set colItemSet = objWMIService.execQuery _
            ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
        For Each colItem In colItemSet              ' There should only be one active one
            If Not IsNull(colItem.IPAddress) Then
                For i = LBound(colItem.IPAddress) To UBound(colItem.IPAddress)
                    If colItem.IPAddress(i) <> "0.0.0.0" Then   'any other connections to a PC such as bluetooth create a blank IP
                        If colItem.IPAddress(i) = "192.168.100.104" Then
                            Set wNetwork = WScript.CreateObject("WScript.Network")
                              printerPath = "\\sam1\KM-200"
                              wNetwork.AddWindowsPrinterConnection printerPath
                              printerPath2 = "\\sam1\KM-250"
                              wNetwork.AddWindowsPrinterConnection printerPath2
                              printerPath2 = "\\sam1\KM-350"
                              wNetwork.AddWindowsPrinterConnection printerPath2
                              printerPath2 = "\\sam1\KM-420"
                              wNetwork.AddWindowsPrinterConnection printerPath2
                              WScript.Quit
                        End If
                    End If
                Next
            End If
        Next
    End If

any help or suggestions will be greatly appreciated
 
Last edited by a moderator:
Oi what a mess.. give me a couple hours and I'll post something cleaner

question: why are you using the goto 0 statement in vbscript? VB != VBScript and, in general, is not the way to handle errors in VBScript.. that line will turn error trapping off..

edit:
try this:
Code:
Dim strComputer,strIPAddress,strIPRange
Dim objNetwork
Dim IPConfigSet, IPConfig
Dim arrIPAddress

strComputer = "."

Set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" _ 
 & strComputer & "/root/cimv2").ExecQuery("select IPAddress from " _ 
 & "Win32_NetworkAdapterConfiguration where IPEnabled=True") 
 
Set objNetwork = WScript.CreateObject("WScript.Network")

For Each IPConfig In IPConfigSet
     If Not IsNull(IPConfig.IPAddress) Then
          arrIPAddress = split(IPConfig.IPAddress(0) ,".")
          strIPRange = arrIPAddress(0) & "." & arrIPAddress(1) & "." & arrIPAddress(2) & ".0"	
          select case strIPRange
               case "192.168.100.0"
                    objNetwork.AddWindowsPrinterConnection "\\server\printer"
               case "192.168.0.0"
                    objNetwork.AddWindowsPrinterConnection "\\server\printer"
          end select
     end if
next

set objNetwork = nothing
set IPConfigSet = nothing
 
Last edited:

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back