Solved

Convert Case To if Statement on vbscript

Posted on 2014-09-22
1
228 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
1 Comment
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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