• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

VBS Printer Login Script Based on IP


OSNN Junior Addict

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

currently we use two ip ranges

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.
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) <> "" Then   'any other connections to a PC such as bluetooth create a blank IP
                        If colItem.IPAddress(i) = "" 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
                        End If
                    End If
            End If
    End If
any help or suggestions will be greatly appreciated
Last edited by a moderator:


Woah.. I'm still here?
Staff member
Political User
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..

try this:
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 ""
                    objNetwork.AddWindowsPrinterConnection "\\server\printer"
               case ""
                    objNetwork.AddWindowsPrinterConnection "\\server\printer"
          end select
     end if

set objNetwork = nothing
set IPConfigSet = nothing
Last edited:

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
What a long strange trip it's been. =)

Forum statistics

Latest member