当前位置:网站首页>Restore the default routing settings of the primary network card

Restore the default routing settings of the primary network card

2022-06-23 08:43:00 shawyang

As Administrator powershell Execute these commands

$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").DHCPServer

$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 Some of the properties of the data structure are the same , but win32-networkadapterconfiguration There is no in the data structure of NetConnectionID,win32-networkadapter Only in the data structure of NetConnectionID

$interfacename=Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True' | Select -ExpandProperty NetConnectionID

#$interfacename=(((netsh interface show interface)[3] -replace ' ', ';') -replace ';;;', '').split(";")[-1].trim(" ")

#$interfacename=(Get-NetAdapter -InterfaceIndex $idx).Name

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

The last two sentences depend on the needs

netsh interface set interface $interfacename disabled

netsh interface set interface $interfacename enabled

Reference resources

https://www.powershellgallery.com/packages/PCHardwareConfiguration/1.0.0/Content/Scripts%5CGet-PCNetworkConfiguration.ps1

https://www.powershellgallery.com/packages/PSWinDocumentation/0.1.0/Content/Private%5CComputers.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'"

or

$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'

or

$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;

If there is only one network card , This code can get the local connection name , General Chinese and English version 2008R2-2022 System ( contain Win7-Win11)

(Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True').NetConnectionID

原网站

版权声明
本文为[shawyang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201101847379313.html