.vbs login script to run file based on IP address

Hello Experts,
I'm here yeilding to your knowledge.  I have a situation where I need some assistance building a .vbs script to do (what I believe) is a simple task.

I have a scenario which I need to have a .vbs script run on my user's login.  The script should take a look at the IP address of the current machine and based on that address make a decision to run 1 of 2 files.

In my head this is how I envision the code..

- User initiates login
- (this .vbs runs)
- the script will check current IP addresses on the system.
- the script will make a decision (it will challenge the current IP to a set of known static IPs)
- the script (based on the desision) will then run (File A) if the current IP matches the known static IPs or run (File B) if the current IP does not match the known static IPs.

Known static addresses:

Does anyone have a simple code to share with that setup?

Who is Participating?
rlandquistConnect With a Mentor Commented:
OK, Here is a solution that I think will work.
The script get all the ip addresses of the computer.  It ignores
Then it runs the function to check the format.  If the format meeets IPv4 then it exits checking and goes to the select step.

This should work except in the situation where a pc has 2 or more ipv4 addresses, but that is not too normal.

Let me know what you think.
Set objNetwork = CreateObject("Wscript.Network")
Set arrList = CreateObject("System.Collections.ArrayList")

strComputer = "."

'Connect to computer WMI Service
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Query computer network adapter configurations for ip addresses and add to array
Set IPConfigSettings = objWMIService.ExecQuery _
( "Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE" )
For Each IPConfig In IPConfigSettings
    If Not IsNull(IPConfig.IPAddress) Then
        For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
            If IPConfig.IPAddress(i) <> "" And _
                Not arrList.Contains(IPConfig.IPAddress(i)) Then
                arrList.Add IPConfig.IPAddress(i)
            End If
    End If

'Display IP Addresses
For Each strAddress In arrList
    If is_ip(strAddress) Then
        Exit For
    End If

Select Case strAddress
    Case "", "", ""
        WScript.Echo "run process A"
    Case Else
        WScript.Echo "run process B"
End Select

Function is_ip(ip_adress)
    is_ip = True
    ' split string into list
    ip_adress_lst = Split(ip_adress, ".")
    ' if number of IP adress parts is 4
    If UBound(ip_adress_lst) = 3 Then
        ' validate if every IP part is numeric
        For Each ip_part In ip_adress_lst
            If Not IsNumeric(ip_part) Then 
                is_ip = False
                Exit For
            End If
        ' if number of IP adress parts is not 4
        is_ip = False
    End If
End Function

Open in new window

irishmic33Author Commented:
Ha... yeh, well thats actually a question I asked about a year ago here on experts exchange.  While that issue was dealing with a range instead of specific static IP addresses.  BTW that solution works perfect for testing the /24 networks.  

Funny you found that one.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Well the first part of your solution would be to get the ip address.  I will upload an example for this, however, there could be multiple addresses for a computer.

Once you have the ip address it is just a matter of using the select case statement.
irishmic33Author Commented:
hmm... you are right.  I wonder what the best way to handle multiple addresses would be.  
I am doing testing on my Win7 machine and I am getting the IPv6 address in response.  I will keep looking into this and let you know what I find.
irishmic33Author Commented:
Ok, i'm trying it now.  Thanks for your help btw.   I'm pretty wet behind the ears with .vbs.  I can read it, just can't write it.

irishmic33Author Commented:
Works like a charm.  

Thanks rlandquist.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.