【问题标题】:windows use wmi to set network configurationwindows使用wmi设置网络配置
【发布时间】:2014-01-17 02:25:45
【问题描述】:

我最近在做一个项目,需要在安装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)?

很多吗?

【问题讨论】:

  • 怎么会“不行”?那会发生什么?没有?配置了错误的适配器?你的系统死机了?还有什么?

标签: windows networking powershell


【解决方案1】:

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

例如:

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

AdapterTypeEthernet 802.3 (AdapterTypeId=0) 和 speed>500000000 允许我消除 WIFI 以太网接口,但大多数时候我使用 NetConnectionID这是你可以给接口起的名字(一种 eth0)

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2023-03-24
    • 2016-01-11
    • 2018-08-24
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多