?
Solved

Convert Case To if Statement on vbscript

Posted on 2014-09-22
1
Medium Priority
?
230 Views
Last Modified: 2014-09-22
Hi

how can i convert this script from case to If


Option Explicit
On Error Resume Next

Select Case DefaultGateway
       
        Case "10.2.254.1"
                'Do something
        Case "10.3.254.1"
                'Do something
        Case "10.4.254.1"
                'Do something
        Case "10.5.254.1"
                'Do something
        Case "10.6.254.1"
                'Do something
End Select

Function DefaultGateway
        DefaultGateway = Empty
        Dim oDG, oDGs, WMI
        Set WMI = GetObject("winmgmts:\\.\root\cimv2")          
        Set oDGs = WMI.ExecQuery _          
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        For Each oDG In oDGs
                If Not IsNull(oDG.DefaultIPGateway) Then
                If Not oDG.defaultIPGateway(0) = "0.0.0.0" Then
                DefaultGateway = oDG.DefaultIPGateway(0)
                        Exit For
                        End If
                        End If
        Next
End Function
0
Comment
Question by:David Regev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 27

Accepted Solution

by:
MacroShadow earned 2000 total points
ID: 40336434
Not sure why you would want to change it, Select Case is the structure to use when dealing with many possibilities.

Regardless, this is what you want.
Option Explicit
On Error Resume Next

If DefaultGateway = "10.2.254.1" Then
                'Do something
ElseIf DefaultGateway = "10.3.254.1" Then
                'Do something
ElseIf DefaultGateway = "10.4.254.1" Then
                'Do something
ElseIf DefaultGateway = "10.5.254.1" Then
                'Do something
ElseIf DefaultGateway = "10.6.254.1" Then
                'Do something
End If

Function DefaultGateway
        DefaultGateway = Empty
        Dim oDG, oDGs, WMI
        Set WMI = GetObject("winmgmts:\\.\root\cimv2")          
        Set oDGs = WMI.ExecQuery _          
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
        For Each oDG In oDGs
                If Not IsNull(oDG.DefaultIPGateway) Then
                If Not oDG.defaultIPGateway(0) = "0.0.0.0" Then
                DefaultGateway = oDG.DefaultIPGateway(0)
                        Exit For
                        End If
                        End If
        Next
End Function 

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question