我最近在做一个项目,需要安装windows系统后自动设置networking信息。 我做了这个使用PowerShell脚本,但没有默认的networking适配器,如etho在Linux中,所以我必须将所有networking适配器设置为相同的IP / DNS /等。 这里是代码:
$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默认有线networking(如在Linux中的eth0)?
许多thx?
你的问题是, win32_networkadapterconfiguration
返回所有的网络win32_networkadapterconfiguration
。 您可以先使用Win32_NetworkAdapter来筛选要使用的适配器。
例如 :
Get-WmiObject Win32_NetworkAdapter -Filter "AdapterTypeId=0 AND speed>500000000"
给AdapterType
值以太网802.3 (AdapterTypeId = 0)和速度> 500000000允许我消除WIFI以太网接口,但大部分时间我使用NetConnectionID这是您可以给接口(种类eth0)
$networkAdapter = Get-WmiObject Win32_NetworkAdapter -Filter "NetConnectionID='NET1'"
一旦你选择了适配器,你可以选择网络配置
get-wmiobject -class win32_networkadapterconfiguration -Filter "index=$($networkAdapter.Index)"