gpt4 book ai didi

windows使用wmi设置网络配置

转载 作者:可可西里 更新时间:2023-11-01 11:54:32 25 4
gpt4 key购买 nike

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

非常感谢?

最佳答案

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

例如:

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

AdapterType 提供值 Ethernet 802.3 (AdapterTypeId=0) 和 speed>500000000 允许我消除 WIFI 以太网接口(interface),但大多数我使用 NetConnectionID 的时间是您可以为接口(interface)指定的名称(类似于 eth0)

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

enter image description here

一旦你选择了适配器,你就可以选择网络配置

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

关于windows使用wmi设置网络配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com