Solved

Convert Case To if Statement on vbscript

Posted on 2014-09-22
1
221 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 26

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now