Solved

Convert Case To if Statement on vbscript

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

705 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