cizixs
cizixs

Reputation: 13991

windows use wmi to set network configuration

I recently work on a project that needs to set network info automatically after installing windows system. I did this use powershell script, but there is no default network adapter like etho in linux, so I have to set all network adapter to the same ip/dns/etc. Here is the code:

$nic1IP=[string] $args[0]
$nic1Mask=[string] $args[1]
$nic1GW=[string] $args[2]

if ( ("$nic1IP" -ne $null) -and ("$nic1Mask" -ne $null) )
{
    $wmiList=[Array](get-wmiobject -class win32_networkadapterconfiguration -filter ipenabled=true -computername .)
    foreach ($nicItem in $wmiList)
    {  
        $nicItem.EnableStatic($nic1IP, $nic1Mask)
    if ( "$nic1GW" -ne $null)
    {
            $nicItem.SetGateways($nic1GW)
    }
    }
}

The problem is: sometimes it won't work!

my question is, is there a way to set windows default wired network(like eth0 in linux)?

Many thx?

Upvotes: 1

Views: 1173

Answers (1)

JPBlanc
JPBlanc

Reputation: 72680

Your problem is that win32_networkadapterconfiguration return all the network adaptaters. You can first use Win32_NetworkAdapter to filter the adaptaters you want to work with.

for example :

Get-WmiObject Win32_NetworkAdapter -Filter "AdapterTypeId=0 AND speed>500000000"

gives AdapterType with value Ethernet 802.3 (AdapterTypeId=0) and speed>500000000 allow me to eliminate WIFI ethernet interface, but most of the time I use NetConnectionID wich is the name you can give to the interface (kind of eth0)

$networkAdapter = Get-WmiObject Win32_NetworkAdapter -Filter "NetConnectionID='NET1'"

enter image description here

Once you adapter choosen you can choose the network configuration

get-wmiobject -class win32_networkadapterconfiguration -Filter "index=$($networkAdapter.Index)"

Upvotes: 2

Related Questions