VBS Printer Login Script Based on IP

Discussion in 'Windows Server Systems' started by illmaticone, Jan 13, 2009.

  1. illmaticone

    illmaticone OSNN Junior Addict

    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: Jan 13, 2009
  2. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    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: Jan 14, 2009