我最近在做一个项目,安装windows系统后需要自动设置网络信息。我使用了powershell脚本,但是在linux中没有默认的网络适配器,比如eto,所以我必须将所有网络适配器设置为相同的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)?
很多THX?
最佳答案
您的问题是win32_networkadapterconfiguration
返回所有网络适配器。您可以首先使用win32_networkadapter筛选要使用的适配器。
例如:
Get-WmiObject Win32_NetworkAdapter -Filter "AdapterTypeId=0 AND speed>500000000"
提供值为以太网802.3(AdapterTypeID=0)且速度>500000000的
AdapterType
允许我消除WiFi以太网接口,但大多数情况下,我使用的NetconnectionID是您可以提供给接口的名称(eth0类型)$networkAdapter = Get-WmiObject Win32_NetworkAdapter -Filter "NetConnectionID='NET1'"
一旦选择了适配器,就可以选择网络配置
get-wmiobject -class win32_networkadapterconfiguration -Filter "index=$($networkAdapter.Index)"