Solved

VB Script to check Default Gateway if corrent run if not end

Posted on 2008-10-22
4
726 Views
Last Modified: 2012-05-05
I'm creating a new logon script. I need the script to check the default gateway of the machine and if it correct against what i have set then to continue but if it is wrong to end the script.

0
Comment
Question by:JHG-IT
[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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Brum07
ID: 22774956
Try this;

From here;

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23706347.html

This will check if the gateway is 192.168.10.254, if so show a message box if not it will end the script.

Regards
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.DefaultIPGateway) Then 
        For i=LBound(IPConfig.DefaultIPGateway) to UBound(IPConfig.DefaultIPGateway)
            Select Case IPConfig.DefaultIPGateway(i)
                  Case "192.168.10.254"
                        MsgBox "Your Gateway is 192.168.10.254"
                  Case Else
                        Wscript.Quit
            End Select        
        Next
    End If 
Next

Open in new window

0
 
LVL 3

Expert Comment

by:Angelized
ID: 22775084
Here we go.
Note :  this will only works for static configuration (DHCP should set thet Gateway anyway)
 

On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetCards = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
 
For Each objNetCard in colNetCards
    arrGateways = Array("192.168.1.100")    
    If objNetCard.DefaultIPGateway(0) <> "192.168.1.100" Then objNetCard.SetGateways(arrGateways)
Next

Open in new window

0
 

Author Comment

by:JHG-IT
ID: 22783219
Hi
thanks works a treet. However I'm a complete novice with these scripts and I cant get the default gateway section to link in with my current logon script. If you could help me with this it would be greatly appriecated.
Option Explicit
Dim objNetwork, objUser, CurrentUser, objShell
Dim strGroup, strDrive, objUNC
Dim strRemotePath, strDriveLetter, strNewName, strComputer
 
' Check Default Gateway
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.DefaultIPGateway) Then 
        For i=LBound(IPConfig.DefaultIPGateway) to UBound(IPConfig.DefaultIPGateway)
            Select Case IPConfig.DefaultIPGateway(i)
                  Case "10.103.15.254"
                  Case Else
                        Wscript.Quit
            End Select        
        Next
    End If 
Next
 
 
' Initialise Groups with Const
Const Public_Group = "cn=basswater (public),ou=svr-vic-melb-desal,ou=vic,ou=staff,dc=jhg,dc=com,dc=au"
 
' Create objects and extract strGroup values
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(CurrentUser.MemberOf))
 
' Delete any mapped drives
objShell.Run "net use * /d /y", 1, True
Set objShell = Nothing
 
' If logic testing strGroup for the values in Const groups
If InStr(strGroup, LCase(Public_Group)) Then
objNetwork.MapNetworkDrive "p:", "\\svr-vic-melb-desal\public"
Set objShell = CreateObject("Shell.Application")
End If
 
' Rename Public
strDriveLetter = "p:"
strNewName = "PUBLIC on svr-vic-melb-desal"
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
 
' Network Drive Scanning
If InStr(strGroup, LCase(Public_Group)) Then
objNetwork.MapNetworkDrive "s:", "\\svr-vic-desal-ps1\scanning"
Set objShell = CreateObject("Shell.Application")
End If
 
' Rename Scanning
strDriveLetter = "s:"
strNewName = "SCANNING on svr-vic-desal-ps1"
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
 
' End VBScript .

Open in new window

0
 
LVL 13

Accepted Solution

by:
Brum07 earned 125 total points
ID: 22783686
Should be something like this;

Regards
Option Explicit
Dim objNetwork, objUser, CurrentUser, objShell
Dim strGroup, strDrive, objUNC
Dim strRemotePath, strDriveLetter, strNewName, strComputer
 
' Check Default Gateway
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.DefaultIPGateway) Then 
        For i=LBound(IPConfig.DefaultIPGateway) to UBound(IPConfig.DefaultIPGateway)
            Select Case IPConfig.DefaultIPGateway(i)
                  Case "10.103.15.254"
                        ' Initialise Groups with Const
Const Public_Group = "cn=basswater (public),ou=svr-vic-melb-desal,ou=vic,ou=staff,dc=jhg,dc=com,dc=au"
 
' Create objects and extract strGroup values
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(CurrentUser.MemberOf))
 
' Delete any mapped drives
objShell.Run "net use * /d /y", 1, True
Set objShell = Nothing
 
' If logic testing strGroup for the values in Const groups
If InStr(strGroup, LCase(Public_Group)) Then
objNetwork.MapNetworkDrive "p:", "\\svr-vic-melb-desal\public"
Set objShell = CreateObject("Shell.Application")
End If
 
' Rename Public
strDriveLetter = "p:"
strNewName = "PUBLIC on svr-vic-melb-desal"
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
 
' Network Drive Scanning
If InStr(strGroup, LCase(Public_Group)) Then
objNetwork.MapNetworkDrive "s:", "\\svr-vic-desal-ps1\scanning"
Set objShell = CreateObject("Shell.Application")
End If
 
' Rename Scanning
strDriveLetter = "s:"
strNewName = "SCANNING on svr-vic-desal-ps1"
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
                  Case Else
                        Wscript.Quit
            End Select        
        Next
    End If 
Next

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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