Jump to content

Determine active network connection


Theo
 Share

Recommended Posts

I am writing a Windows 2008 Server follow-up script for my group. This essentially runs after the initial build to perform customizations for the environment, software installations etc. (Why they refuse to use deployment services I will never understand). Anyway, I need to perform some actions on the NIC that has the wire plugged into it. Some of these servers have up to 8 network ports. Windows never seems to name them the same either. So even if our documentation says "plug the public net wire into the on-board NIC port 0" it still may come up as "Local Area Connection 3" or "Local Area Connection 6" or whatever.

I wanted to be able to run some configuration commands, such as:

Run(@ComSpec & " /c " & 'netsh interface set interface name="Local Area Connection" newname="Public"', "", @SW_HIDE)

But...I cannot do this unless I can determine which is the correct connection. any ideas?

Systems AdministratorSiemens: Windows Server SupportFly Fisher, Table Tennis Junkie, and Struggling Coder

Link to comment
Share on other sites

Hi.

...

But...I cannot do this unless I can determine which is the correct connection. any ideas?

Not sure, if this will help you, but you could check the output of devcon.exe

devcon find *

For a Win XP PC the NIC entries look like this:

ROOT\MS_PSCHEDMP\0000 -> clear text name

ROOT\MS_PSCHEDMP\0001 -> clear text name

ROOT\MS_PSCHEDMP\0002 -> clear text name

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

I wanted to be able to run some configuration commands, such as:

Run(@ComSpec & " /c " & 'netsh interface set interface name="Local Area Connection" newname="Public"', "", @SW_HIDE)

But...I cannot do this unless I can determine which is the correct connection. any ideas?

See if this will help. May need some tweaking.

Global $objWMIService, $strIPAddress , $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $NetConnectionID, $Index
Global $strComputer = @ComputerName

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
If $objWMIService = 0 Then
    MsgBox(0, "WMI Error", "Please Fix WMI")

ElseIf $objWMIService <> 0 Then
    _GetIndex()

EndIf

Func _GetIndex()
    ;Gets Index Address based on IPAddress
    $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE', "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $strIPAddress = $objItem.IPAddress(0)
            $IpAddressLen = StringLeft($strIPAddress, 3)
            If $IpAddressLen = "192" Then
                $strIndex = $objItem.Index
                If StringLen($strIndex) = 1 Then $Index = "000" & $strIndex
                If StringLen($strIndex) = 2 Then $Index = "00" & $strIndex
                _NetConnectionID()
                MsgBox(0, "Network Name", $NetConnectionID)

            EndIf
        Next
    EndIf
EndFunc   ;==>_GetIndex

Func _NetConnectionID()
    ;Gets NetConnectionID, ie 'Local Area Connection'
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter where Index=" & $Index, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $NetConnectionID = $objItem.NetConnectionID
        Next
    Else
    EndIf
EndFunc   ;==>_NetConnectionID
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...