windows使用wmi来设置网络配置

时间:2013-12-30 07:51:00

标签: windows networking powershell

我最近在安装Windows系统后需要自动设置网络信息的项目。我这样做使用了powershell脚本,但linux中没有像 etho 这样的默认网络适配器,所以我必须将所有网络适配器设置为相同的ip / dns / etc。这是代码:

$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)
    }
    }
}

问题是:有时它不起作用!

我的问题是,有没有办法设置Windows默认有线网络(如linux中的eth0)?

很多人?

1 个答案:

答案 0 :(得分:2)

您的问题是win32_networkadapterconfiguration返回所有网络适配器。您可以先使用Win32_NetworkAdapter过滤您想要使用的适配器。

例如:

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

AdapterType提供值以太网802.3 (AdapterTypeId = 0)和 speed> 500000000 允许我消除WIFI以太网接口,但大部分时间我使用NetConnectionID,这是你可以给界面的名称(eth0的种类)

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

enter image description here

选择适配器后,您可以选择网络配置

get-wmiobject -class win32_networkadapterconfiguration -Filter "index=$($networkAdapter.Index)"
相关问题