以下代码仅限一块网卡的机器,多块网卡的代码需要调整
以管理员身份powershell执行这几句命令
$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").defaultipgateway
#$idx=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").InterfaceIndex
$idx=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | Where-Object {$_.servicename -eq "netkvm"} ).InterfaceIndex
下面这些开头带#的是备忘的,不用管,跳过即可
#$idx=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | Where-Object {$_.Description -notmatch "NGN"} ).InterfaceIndex
#$idx=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" | Where-Object {$_.Description -match "Tencent"} ).InterfaceIndex
#win32-networkadapter、win32-networkadapterconfiguration 的数据结构有一部分属性是一样的,但win32-networkadapterconfiguration的数据结构里没有NetConnectionID,win32-networkadapter的数据结构里才有NetConnectionID
$interfacename=Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True' | Select -ExpandProperty NetConnectionID
#$interfacename=(((netsh interface show interface)[3] -replace ' ', ';') -replace ';;;', '').split(";")[-1].trim(" ") 如果主网卡名称前面有空格,这空格也是网卡name的一部分,这句代码输出的结果会自动剔除网卡名称左边的空格,结果不准确,因此注释这句,之所以添到这里,是记录下练习语法的过程,如果网卡名称前面没有空格,这句代码至少还是在没有借助$idx的情况下起作用的嘛
#$interfacename=(Get-NetAdapter -InterfaceIndex $idx).Name 需要借助$idx,不如上面黑体那句来得直接,因此注释
route delete 0.0.0.0 mask 0.0.0.0 2>$null
route add 0.0.0.0 mask 0.0.0.0 $gw metric 1 IF $idx 2>$null
route add -p 0.0.0.0 mask 0.0.0.0 $gw metric 1 IF $idx 2>$null
最后两句根据需求而定
netsh interface set interface $interfacename disabled
netsh interface set interface $interfacename enabled
参考
https://www.powershellgallery.com/packages/PCHardwareConfiguration/1.0.0/Content/ScriptsGet-PCNetworkConfiguration.ps1
https://www.powershellgallery.com/packages/PSWinDocumentation/0.1.0/Content/PrivateComputers.ps1
https://gist.github.com/milesgratz/0285a6c3e9dd2bcfbbc72b441fcb6410
https://www.bookstack.cn/read/powershell-networking-guide/manuscript-renaming-the-network-adapter.md
https://mac-blog.org.ua/powershell-get-ip-adresses-by-connection-name
https://newbedev.com/command-to-find-network-interface-for-ip
https://docs.microsoft.com/zh-tw/windows/win32/cimwin32prov/win32-networkadapterconfiguration
$networkconfig=gwmi -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
或
$networkconfig=Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$networkconfig.Caption;
$networkconfig.Description;
$networkconfig.SettingID;
$networkconfig.DatabasePath;
$networkconfig.DHCPEnabled;
$networkconfig.DHCPLeaseExpires;
$networkconfig.DHCPLeaseObtained;
$networkconfig.DHCPServer;
$networkconfig.DNSEnabledForWINSResolution;
$networkconfig.DNSHostName;
$networkconfig.DomainDNSRegistrationEnabled;
$networkconfig.FullDNSRegistrationEnabled;
$networkconfig.Index;
$networkconfig.InterfaceIndex;
$networkconfig.IPConnectionMetric;
$networkconfig.IPEnabled;
$networkconfig.IPFilterSecurityEnabled;
$networkconfig.MACAddress;
$networkconfig.ServiceName;
$networkconfig.TcpipNetbiosOptions;
$networkconfig.WINSEnableLMHostsLookup;
https://docs.microsoft.com/zh-tw/windows/win32/cimwin32prov/win32-networkadapter
$network=Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True'
或
$network=gwmi -Query "SELECT * FROM Win32_NetworkAdapter WHERE NetEnabled = 'True'"
$network.AdapterType;
$network.AdapterTypeID;
$network.Availability;
$network.Caption;
$network.ConfigManagerErrorCode;
$network.ConfigManagerUserConfig;
$network.CreationClassName;
$network.Description;
$network.DeviceID;
$network.GUID;
$network.Index;
$network.Installed;
$network.InterfaceIndex;
$network.MACAddress;
$network.Manufacturer;
$network.MaxNumberControlled;
$network.Name;
$network.NetConnectionID;
$network.NetConnectionStatus;
$network.NetEnabled;
$network.PhysicalAdapter;
$network.PNPDeviceID;
$network.PowerManagementSupported;
$network.ProductName;
$network.ServiceName;
$network.Speed;
$network.SystemCreationClassName;
$network.SystemName;
$network.TimeOfLastReset;
这句代码可以获取本地连接名称,通用中英文版2008R2-2022系统(含Win7-Win11)
代码语言:javascript复制(Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True').NetConnectionID
下面这句适用2012-2022(Win8-Win11)
代码语言:javascript复制(Get-NetAdapter).name