Jump to content

Recommended Posts

Posted

Hey guys,

I need to run a script on several computers that can identify the active network connection, then for each one of them:

- if the network connection is not using DHCP (fixed IP address), change the current DNS server to another one

- if the network connection is using DHCP, do nothing.

I don't know the names of the network connections on the different computers, so the script has to find this by itself.

I've been searching about this but I'm having troubles finding a working solutions.

Any help would be greatly appreciated!

TIA

Posted

As well as netsh.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Give ScriptOMatic a try (see the Example Script section).

Selecting WMI Class "Win32_NetworkAdapterConfiguration" should give you the needed information.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Thanks for your answers :)

@JLogan3o13 > Since I didn't find a way to identify the name of the active network cards I didn't try anything yet ^^

@guinness > I saw before posting that netsh can indeed be used to setup a dns server on a network connection, but you first need to know the connection name to use it!

Thanks for the advice on the WMI stuff, i'll look into it :)

Posted (edited)

Damn this WMI stuff is amazing, thanks again guys :)

I managed to do what I wanted like this:

#requireadmin

#include <Array.au3>

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$query = ""

Local $active_netword_cards[1]
Local $network_cards_to_setup[1]

$active_netword_cards[0]=""
$network_cards_to_setup[0]=""

;getting a list of all network cards

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
     
      if $objItem.NetConnectionStatus == "2" OR $objItem.NetConnectionStatus == "9" Then ;if the network connection is active, we add the index of the network card and the connection name to $active_netword_cards array
         
         _arrayAdd($active_netword_cards, $objItem.Index)
         _arrayAdd($active_netword_cards, $objItem.NetConnectionID)
         
      endif

   Next
  
Endif

;getting settings from all network cards in the array $active_netword_cards

for $i = 1 to UBound($active_netword_cards) - 1 step 2
  
   $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & $active_netword_cards[$i], "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
   
   For $objItem In $query

   if $objItem.DHCPEnabled == "False" Then _arrayAdd($network_cards_to_setup, $active_netword_cards[$i+1]) ;if DHCP is disabled, we add the network card name in the $network_cards_to_setup array
  
   next
  
Next

;setting up primary DNS server of all network cards in the $network_cards_to_setup array
;DNS server used in this example is 10.10.2.45

for $i = 1 to UBound($network_cards_to_setup) - 1 step 1
  
   Runwait('netsh interface IP ADD DNS "'& $network_cards_to_setup[$i] &'" 10.10.2.45 index=1')
  
Next
Edited by Neutro

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
  • Recently Browsing   0 members

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